SHA-based subclass for random module

Raymond Hettinger python at rcn.com
Tue Mar 23 10:11:03 CET 2004


[Trevor Perrin]
> > Actually, Random could be made even easier to subclass.  getrandbits
> > (which is new in 2.4) returns a long.  It would be better for us if
> > there was a getrandstring() function that an underlying generator
> > could implement.  This would also be a helpful addition to the API, at
> > least for cryptographic uses.

[Paul Rubin]
> I agree with this, and was going to propose the same thing.
> getrandbits can be defined easily in terms of getrandstring, and the
> current sample sharandom implemenation at
> 
>    http://www.nightsong.com/phr/python/sharandom.py
> 
> does something along those lines.

If you're sure you need this, please put a feature request on SF.  It
needs to have a use case other than feeding message digest funtions --
that need is readily met with str(x) or the faster hex(x).

The new genrandbits() method was designed around the use case of
making randrange() work over very large intervals which was necessary
for creating large primes for RSA work.  In addition, all of the
methods internal to the Random module work with numbers instead of
byte strings.

Also, genrandbits() was selected because it is straight-forward to
implement for many different random number generators including those
that generate a bit at a time.


Raymond



More information about the Python-list mailing list