random 64-bit int

Tim Peters tim at zope.com
Thu Jul 25 17:07:21 EDT 2002


[Jeff Davis]
> ...
> random() isn't really all that random either, since it is only zero to
> one and a conversion to a float loses a lot of information (I think...
> let me know if I'm mistaken).

A Python float is a C double, which on most boxes has 53 bits of precision.
That means, e.g., that there are 2**53 distinct Python floats in the range
[0.5, 1.0) alone.  Also 2**53 distinct Python floats in the range [0.25,
0.5) alone; etc.  That's a lot of floats.  Python's Wichmann-Hill generator
has only about 45 bits of internal state, and no information is lost in
representing one of those states as a Python float.

A direct and quicker pure-Python method of generating random N-bit ints can
be found here:

    http://www.faqts.com/knowledge_base/view.phtml/aid/4406





More information about the Python-list mailing list