On Mon, 26 Dec 2011 03:11:56 +1100, Chris Angelico wrote:
> On Mon, Dec 26, 2011 at 2:46 AM, Steven D'Aprano
> <[email protected]> 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.
In this case, the source explicitly tells you that the API includes
support for arbitrary large ranges if you include a getrandbits() method:
Optionally, implement a getrandbits() method so that randrange()
can cover arbitrarily large ranges.
I call that a pretty strong guarantee.
> 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.
Um, I'm not sure what you consider "the simple option" in this context. I
would hope you mean to use the high level API of randint:
# need a random number with exactly 20 decimal digits
random.randint(10**20, 10**21-1)
rather than manually assembling a 20 digit number from smaller pieces.
--
Steven
--
http://mail.python.org/mailman/listinfo/python-list