Random string of digits?

Chris Angelico rosuav at gmail.com
Sun Dec 25 22:43:35 EST 2011


On Mon, Dec 26, 2011 at 2:00 PM, Steven D'Aprano
<steve+comp.lang.python at pearwood.info> wrote:
> The implementation of getrandbits is not documented at all. You would
> have to read the C source of the _random module to find out how the
> default pseudo-random number generator produces random bits. But note the
> leading underscore: _random is a private implementation detail.
>
> However the existence and use of random.Random.getrandbits is public,
> documented, and guaranteed.

It's that last line where I find disagreement between documentation
and source code.

"""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. Optionally, a
new generator can supply a getrandbits() method — this allows
randrange() to produce selections over an arbitrarily large range."""

My reading of this is that, if I were to write my own generator, I
could provide that method and gain perfection. It's not until you dig
somewhat that you find out that the default generator actually does
provide getrandbits, meaning that the default randrange can actually
be used for arbitrarily large ranges. Yes, it IS documented, but in
separate places and somewhat as an afterthought; in the 2.x
documentation it's shown as "new in 2.4", which explains this.

Unfortunately my brain isn't really running on all cylinders at the
moment and I can't come up with a better wording, but is there some
way this could be stated up in the same top paragraph that explains
about 53-bit precision and 2**19937-1 period? That's where I got my
original information from; I didn't go down to the individual function
definitions, which is the only place that it's stated that the
Mersenne Twister does include getrandbits (and which assumes that
you've read the top section that states that Mersenne Twister is the
default implementation).

ChrisA



More information about the Python-list mailing list