TheDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/alex-ber/AlexBerUtils/llms.txt
Use this file to discover all available pages before exploring further.
randoms module provides the Sampler class for drawing samples from continuous statistical distributions with optional lower/upper bounds. When NumPy is installed samples are drawn via numpy.random.RandomState; otherwise the standard library random module is used transparently.
Installation
If NumPy is not installed, an
OptionalNumpyWarning is issued at import time
and the pure-Python fallback is activated automatically. No code changes are
required.Quick start
Sampler class
Sampler extends BaseSampler and is the class you should instantiate directly.
The concrete implementation chosen at import time depends on whether NumPy is
available:
- NumPy available — uses
numpy.random.RandomStatemethods. - NumPy absent — uses the standard
random.Randommethods.
get_sample() method.
Constructor parameters
Name of the distribution to sample from. Must be one of the nine supported
values listed in the Supported distributions
section below.
Shape parameter. Its meaning depends on the distribution — for log-normal it
is
sigma of the underlying normal; for gamma it is the shape (k)
parameter; for beta it is the first shape parameter alpha.Scale parameter. For log-normal,
exp(mu) (the median). For normal and
Gaussian distributions, the mean (mu). For exponential, the mean of the
distribution directly.Minimum acceptable value. Samples below this bound are discarded and
re-drawn.
None is treated as unbounded (i.e. -math.inf).Maximum acceptable value. Samples above this bound are discarded and
re-drawn.
None is treated as unbounded (i.e. math.inf).Maximum number of draw-and-test attempts before raising
SamplingError.Integer seed for the internal random generator. Mutually exclusive with
random_state / random_instance.(NumPy build only.) An existing
RandomState object to use. Mutually
exclusive with random_seed.(Pure-Python build only.) An existing
random.Random instance to use.
Mutually exclusive with random_seed.get_sample()
Draws and returns a single sample within [lower_bound, upper_bound].
max_retries times. If no in-bounds sample is produced
within that limit, SamplingError is raised.
Supported distributions
| Name | Parameters used | Notes |
|---|---|---|
lognormvariate | shape = sigma, scale = exp(mu) | Log-normal; always positive. |
normalvariate | shape = std dev, scale = mean | Normal (Gaussian) via the random module. |
gauss | shape = std dev, scale = mean | Gaussian; faster than normalvariate in some implementations. |
expovariate | scale = mean | Exponential; always positive. NumPy uses scale directly; pure-Python uses 1/scale internally. |
vonmisesvariate | shape = kappa, scale = mu | Circular distribution for angles. |
gammavariate | shape = k (alpha), scale = theta | Gamma distribution. |
betavariate | shape = alpha, scale = beta | Beta distribution; values in (0, 1). |
paretovariate | shape = alpha | Pareto distribution; scale is unused. |
weibullvariate | shape = k, scale = lambda | Weibull distribution. |
Examples
Normal distribution with bounds
Reusing an existing random state (NumPy)
Reusing a random.Random instance (pure Python)
Error handling
SamplingError
Raised when get_sample() cannot produce an in-bounds value within max_retries attempts.
SamplingError attributes:
| Attribute | Type | Description |
|---|---|---|
distribution | str | The distribution that was attempted. |
retries | int | Number of attempts made. |
lower_bound | float | None | The configured lower bound (None if unbounded). |
upper_bound | float | None | The configured upper bound (None if unbounded). |
OptionalNumpyWarning
Issued at import time when NumPy is not installed. The warning class is defined in
alexber.utils.warning. To suppress it:
BaseSampler
BaseSampler contains the validation logic shared by both the NumPy and
pure-Python Sampler implementations. You will not instantiate it directly, but
you can subclass it to create custom samplers:
BaseSampler.__init__:
distributionmust be inBaseSampler.supported_distributions.lower_boundmust be strictly less thanupper_bound.- At most one of
random_seed/random_state/random_instancemay be set.
