Random Number Generators#

Counter-based#

heptools.math.random.SeedLike[source]#

A seed or a sequence of seeds.

Type:

int, str, Iterable[int or str]

class heptools.math.random.CBRNG(*seed)[source]#

Counter-based random number generator (CBRNG).

Parameters:

seed (SeedLike) – The seed used to generate the key.

uniform(counters, low=0.0, high=1.0)[source]#

Generates from uniform distribution.

Parameters:
  • counters (ArrayLike) – The counter array.

  • low (float, optional) – The lower (closed) bound of the distribution.

  • high (float, optional) – The upper (open) bound of the distribution.

Returns:

ndarray – The random sample.

normal(counters, loc=0.0, scale=1.0)[source]#

Generates from normal distribution using Box-Muller transform [1].

Warning

In an extremely rare case (\(1/2^{53}\)), it may raise a “divide by zero” warning.

Parameters:
  • counters (ArrayLike) – The counter array.

  • loc (float, optional) – The mean of the normal distribution.

  • scale (float, optional) – The standard deviation of the normal distribution.

Returns:

ndarray – The random sample.

References

choice(counters, a, p=None)[source]#

Generates a random sample from a given array.

Parameters:
  • counters (ArrayLike) – The counter array.

  • a (ArrayLike or int) – If an array-like object, a random sample is generated by choosing from the first dimension. If an int, the random sample is generated as if it were np.arange(a).

  • p (ArrayLike, optional) – The probabilities associated with each entry in a. If not given, the sample assumes a uniform distribution over all entries.

Returns:

ndarray – The random sample.

Notes

If the counters has shape \([x_{1}, x_{2}, ..., x_{n}]\) and a has shape \([y_{1}, y_{2}, ..., y_{m}]\), the output shape is given as follows:

\[\begin{split}&[x_{1}, x_{2}, ..., x_{n-1}, y_{2}, ..., y_{m}] & n > 1 \land m > 1\\ &[x_{1}, x_{2}, ..., x_{n-1}] & n > 1 \land m = 1\\ &[x_{1}, y_{2}, ..., y_{m}] & n = 1 \land m > 1\\ &[x_{1}] & n = 1 \land m = 1\end{split}\]