2006-04-28 13:54:39

Thread-safe random number generation

Random number generation capabilities, speed, and thread safety in stdlib vary from version to version of Python. In addition, attempts to use an OS-specific random number source can result in unexpected exceptions being raised. Also, a bug in Python 2.3.0 can lead to a reduction in entropy, and a bug in Python 2.4.0 and 2.4.1 can result in exceptions related to open filehandles on some multithreaded Posix platforms.

This module works around as many of these issues as it can by defining random number generator classes that can be used safely by multiple threads, using the best random number sources available. They support all versions of Python from 2.1 up, and fall back on more reliable generators when exception conditions occur. In addition, convenience functions equivalent to random.random() and os.urandom() are exposed.

Copyright 2006 Fourthought, Inc. (USA). Detailed license and copyright information: http://4suite.org/COPYRIGHT Project home, documentation, distributions: http://4suite.org/

The best available OS-agnostic PRNG, thread-safe.

Implements getrandbits() in all versions of Python. Also adds getrandbytes(), which returns a str of bytes.

Gaussian distribution.

mu is the mean, and sigma is the standard deviation.

Thread-safe.

getrandbytes(k) -> x. Returns k random bytes as a str.

Initialize internal state from hashable object.

None or no argument seeds from current time or from an operating system specific randomness source if available.

If a is not None or an int or long, hash(a) is used instead.

A PRNG that uses an OS-specific random number source, if available, falling back on an instance of FtRandom.

Calls to seed(), jumpahead(), getstate() and setstate() only affect the fallback FtRandom instance.

Implements getrandbits() in all versions of Python. Also adds getrandbytes(), which returns a str of bytes.

getrandbits(k) -> x. Generates a long int with k random bits.

getrandbytes(k) -> x. Returns k random bytes as a str.

Return internal state; can be passed to setstate() later.

Make the fallback PRNG (an instance of FtRandom) jump ahead

Get the next random number in the range [0.0, 1.0).

Seed the fallback PRNG (an instance of FtRandom)

Restore internal state from object returned by getstate().

Returns a string of random bytes from the best RNG available. Equivalent to os.urandom(), but failsafe.

Returns a random float, n, where 0 <= n < 1

Gaussian distribution.

mu is the mean, and sigma is the standard deviation.

Thread-safe.

getrandbits(k) -> x. Generates a long int with k random bits.

getrandbytes(k) -> x. Returns k random bytes as a str.

Get the next random number in the range [0.0, 1.0).

urandom(n) -> str

Return a string of n random bytes suitable for cryptographic use.

A PRNG that uses an OS-specific random number source, if available, falling back on an instance of FtRandom.

Calls to seed(), jumpahead(), getstate() and setstate() only affect the fallback FtRandom instance.

Implements getrandbits() in all versions of Python. Also adds getrandbytes(), which returns a str of bytes.
<Ft.Lib.Random.FtSystemRandom object>
['urandom', 'FtRandom', 'FtSystemRandom', 'DEFAULT_RNG', 'Random', 'GetRandomBytes']
True
False
True
True
True
(2, 4, 3, 'final', 0)
False