summaryrefslogtreecommitdiff
path: root/Lib/random.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2004-08-31 01:05:15 +0000
committerRaymond Hettinger <python@rcn.com>2004-08-31 01:05:15 +0000
commitf1ec9d741684cd790c27905f377df5712869c14b (patch)
tree7c688df8762081dd9b1ee58aa880edceaca96326 /Lib/random.py
parent3c0311405145ddd505865508b03102fcc9bc8d2a (diff)
downloadcpython-f1ec9d741684cd790c27905f377df5712869c14b.tar.gz
Take advantage of the math library's ldexp for assembling a float by
components without division and without roundoff error for properly sized mantissas (i.e. on systems with 53 or more mantissa bits per float). Eliminates the previous implementation's rounding bias as aptly demonstrated by Tim Peters.
Diffstat (limited to 'Lib/random.py')
-rw-r--r--Lib/random.py8
1 files changed, 3 insertions, 5 deletions
diff --git a/Lib/random.py b/Lib/random.py
index 06990d243a..fd9374a7f1 100644
--- a/Lib/random.py
+++ b/Lib/random.py
@@ -43,7 +43,7 @@ from warnings import warn as _warn
from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType
from math import log as _log, exp as _exp, pi as _pi, e as _e
from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
-from math import floor as _floor
+from math import floor as _floor, ldexp as _ldexp
__all__ = ["Random","seed","random","uniform","randint","choice","sample",
"randrange","shuffle","normalvariate","lognormvariate",
@@ -63,13 +63,11 @@ try:
from binascii import hexlify as _hexlify
except ImportError:
_urandom = None
-else:
- _tofloat = 2.0 ** (-7*8) # converts 7 byte integers to floats
# Translated by Guido van Rossum from C source provided by
# Adrian Baddeley. Adapted by Raymond Hettinger for use with
-# the Mersenne Twister core generator.
+# the Mersenne Twister and os.urandom() core generators.
import _random
@@ -761,7 +759,7 @@ class HardwareRandom(Random):
"""Get the next random number in the range [0.0, 1.0)."""
if _urandom is None:
raise NotImplementedError('Cannot find hardware entropy source')
- return long(_hexlify(_urandom(7)), 16) * _tofloat
+ return _ldexp(long(_hexlify(_urandom(7)), 16) >> 3, -BPF)
def getrandbits(self, k):
"""getrandbits(k) -> x. Generates a long int with k random bits."""