Random string of digits?

Chris Angelico rosuav at gmail.com
Sun Dec 25 11:11:56 EST 2011


On Mon, Dec 26, 2011 at 2:46 AM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> Use the Source, Luke, er, Chris :)
>
> If I've read the source correctly, randint() will generate sufficient
> bits of randomness to ensure that the entire int is random.
>
> http://hg.python.org/cpython/file/default/Lib/random.py

I prefer not to rely on the source. That tells me what happens, not
what's guaranteed to happen. However... bit of poking around can't
hurt. That file doesn't actually justify anything, because
random.Random() does not define getrandbits() - that, it seems, comes
from _random(); turns out that getrandbits is actually doing pretty
much the same thing I suggested:

http://hg.python.org/cpython/file/745f9fd9856d/Modules/_randommodule.c#l371

Need a 64-bit random number? Take two 32-bit numbers and concatenate.
So, it's going to be easier and clearer to just take the simple
option, since it's actually doing the same thing underneath anyway.

ChrisA



More information about the Python-list mailing list