pseudo-random number generators for various distributions.
Almost all module functions depend on the basic function random()
, which generates a random float uniformly in the semi-open range [0.0, 1.0).
Python uses the Mersenne Twister as the core generator.
The pseudo-random generators of this module should not be used for security purposes. For security or cryptographic uses, see the secrets
module.
The functions supplied by this module are actually bound methods of a hidden instance of the random.Random
class.
Class Random
can also be subclassed if you want to use a different basic generator of your own devising: in that case, override the random()
, seed()
, getstate()
, and setstate()
methods.
seed: 当设置相同的seed时,可以得到相同的随机数。
1 random.seed(1) 2 a2 = random.random() 3 print(a2) 4 5 random.seed(0) 6 a11 = random.random() 7 print(a11 == a1) 8 9 result: 10 0.8444218515250481 11 0.13436424411240122 12 True