Pseudorandom number generator

This module implements the Mersenne Twister algorithm for pseudo-random number generation. It is based on now-defunct projects by Makoto Matsumoto and Takuji Nishimura, and Jasper Bedaux for the core generator, Egglib version 2 for conversion to other laws than uniform. All non-uniform distribution laws generators are based either on the random.integer_32bit() or the standard (half-open, 32 bit) random.uniform() methods.

Note that the pseudo-random number generator is seeded, by default, using the system clock and that it will yield the exact same sequence of random numbers (regardless of the distribution law used) if the seed is identical. This means that different processes started have the same time will be based on the same sequence of random number. It is possible to get and set the seed at any time (see the corresponding methods below).

The content of the module is listed below:

egglib.random.get_seed()

Get the seed value.

egglib.random.set_seed(seed)

Reset the random number sequence with a seed value.

egglib.random.uniform()

Draw a value in the half-open interval [0,1) with default 32-bit precision.

egglib.random.uniform_53bit()

Draw a value in the half-open interval [0,1) with increased 53-bit precision.

egglib.random.uniform_closed()

Draw a value in the closed interval [0,1].

egglib.random.uniform_open()

Draw a value in the open interval (0,1).

egglib.random.integer(n)

Draw an integer from a uniform distribution.

egglib.random.integer_32bit()

Draw a 32-bit random integer.

egglib.random.boolean()

Draw a boolean with equal probabilities (p = 0.5).

egglib.random.bernoulli(p)

Draw a boolean with given probability.

egglib.random.binomial(n, p)

Draw a value from a binomial distribution.

egglib.random.exponential(expectation)

Draw a value from an exponential distribution.

egglib.random.geometric(p)

Draw a value from a geometric distribution.

egglib.random.normal()

Draw a value from the normal distribution.

egglib.random.normal_bounded(expect, sd, ...)

Draw a value from a normal distribution with bounds.

egglib.random.poisson(p)

Draw a value from a Poisson distribution.

egglib.random.get_seed()[source]

Get the seed value.

Returns:

An integer.

egglib.random.set_seed(seed)[source]

Reset the random number sequence with a seed value.

egglib.random.uniform()[source]

Draw a value in the half-open interval [0,1) with default 32-bit precision. The value 1 is not included.

egglib.random.uniform_53bit()[source]

Draw a value in the half-open interval [0,1) with increased 53-bit precision. The value 1 is not included.

The increased precision increases the number of possible values (2^53 = 9007199254740992 instead of 2^32 = 4294967296). This comes with the cost of increased computing time.

egglib.random.uniform_closed()[source]

Draw a value in the closed interval [0,1]. Standard 32-bit precision. Both limits are included.

egglib.random.uniform_open()[source]

Draw a value in the open interval (0,1). Standard default 32-bit precision. Both limits are excluded.

egglib.random.integer(n)[source]

Draw an integer from a uniform distribution.

Parameters:

n – mumber of possible values). Note that this number is excluded and will never be returned. Required to be a stricly positive integer.

Returns:

An integer in range [0, n-1].

egglib.random.integer_32bit()[source]

Draw a 32-bit random integer.

Returns:

An integer in the interval [0, 4294967295] (that is in the interval [0, 2^32-1].

egglib.random.boolean()[source]

Draw a boolean with equal probabilities (p = 0.5).

Returns:

A boolean.

egglib.random.bernoulli(p)[source]

Draw a boolean with given probability.

Parameters:

p – probability of returning True.

Returns:

A boolean.

egglib.random.binomial(n, p)[source]

Draw a value from a binomial distribution.

Parameters:
  • n – Number of tests (>=0).

  • p – Test probability (>=0 and <=1).

Returns:

An integer (number of successes).

egglib.random.exponential(expectation)[source]

Draw a value from an exponential distribution.

Parameters:

expectation – distribution’s mean (equal to 1/\(\lambda\) , if \(\lambda\) is the rate parameter). Required to be >0.

Returns:

An integer.

egglib.random.geometric(p)[source]

Draw a value from a geometric distribution.

Parameters:

p – geometric law parameter (>0 and <=1).

Returns:

A positive integer.

egglib.random.normal()[source]

Draw a value from the normal distribution. Expectation is 0 and variance i 1. The expression rand.normal() * sd + m can be used to rescale the drawn value to a normal distribution with expectation \(m\) and standard deviation \(sd\).

Returns:

A real value.

egglib.random.normal_bounded(expect, sd, mini, maxi)[source]

Draw a value from a normal distribution with bounds.

Params expect:

expectation

Params sd:

standard deviation

Params mini:

minimal value (included)

Params maxi:

maximal value (included)

Returns:

A real number.

egglib.random.poisson(p)[source]

Draw a value from a Poisson distribution.

Parameters:

p – Poisson distribution parameter (usually noted \(\lambda\)). Required to be >0

Returns:

A positive integer.