random — 生成伪随机数

源代码: Lib/random.py


此模块为各种分布,实现伪随机数生成器。

For integers, there is uniform selection from a range. For sequences, there is uniform selection of a random element, a function to generate a random permutation of a list in-place, and a function for random sampling without replacement.

On the real line, there are functions to compute uniform, normal (Gaussian), lognormal, negative exponential, gamma, and beta distributions. For generating distributions of angles, the von Mises distribution is available.

Almost all module functions depend on the basic function random() , which generates a random float uniformly in the half-open range 0.0 <= X < 1.0 . Python uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1. The underlying implementation in C is both fast and threadsafe. The Mersenne Twister is one of the most extensively tested random number generators in existence. However, being completely deterministic, it is not suitable for all purposes, and is completely unsuitable for cryptographic purposes.

The functions supplied by this module are actually bound methods of a hidden instance of the random.Random class. You can instantiate your own instances of Random to get generators that don’t share state.

Random can also be subclassed if you want to use a different basic generator of your own devising: see the documentation on that class for more details.

The random module also provides the SystemRandom class which uses the system function os.urandom() to generate random numbers from sources provided by the operating system.

警告

The pseudo-random generators of this module should not be used for security purposes. For security or cryptographic uses, see the secrets 模块。

另请参阅

M. Matsumoto and T. Nishimura, “Mersenne Twister: A 623-dimensionally equidistributed uniform pseudorandom number generator”, ACM Transactions on Modeling and Computer Simulation Vol. 8, No. 1, January pp.3–30 1998.

Complementary-Multiply-with-Carry recipe for a compatible alternative random number generator with a long period and comparatively simple update operations.

注意

The global random number generator and instances of Random are thread-safe. However, in the free-threaded build, concurrent calls to the global generator or to the same instance of Random may encounter contention and poor performance. Consider using separate instances of Random per thread instead.

簿记函数

random. seed ( a = None , version = 2 )

初始化随机数生成器。

a 被省略或 None , the current system time is used. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability).

a 是 int,它会被直接使用。

With version 2 (the default), a str , bytes ,或 bytearray object gets converted to an int and all of its bits are used.

With version 1 (provided for reproducing random sequences from older versions of Python), the algorithm for str and bytes generates a narrower range of seeds.

3.2 版改变: Moved to the version 2 scheme which uses all of the bits in a string seed.

3.11 版改变: The seed must be one of the following types: None , int , float , str , bytes ,或 bytearray .