I've done a Monte Carlo simulation using Python3's random.random(), and I want to cross-check the result by using a different PRNG, but still with Python. Can this be done?
Apparently(?) random.getrandbits(k) uses Wichmann-Hill instead of MersenneTwister, but this returns an integer with the specified number of bits. Can this be adapted somehow?
Also, I'm using the pypy interpreter because of an approximate 10x speed advantage (but it can't import numpy).
Note that the Twister is used for everything in CPython's random
, including by getrandbits()
.
You can get entirely different random numbers by using methods of the random.SystemRandom
class instead. For example
>>> import random
>>> mygen = random.SystemRandom()
>>> mygen.random()
0.06723509434525832
>>> mygen.randrange(-20, 40)
23
This uses your platform's urandom()
to get bits, which on most platforms is "cryptographically secure". It's slower, and cannot be "seeded" (results are not reproducible).