diff options
author | Guido van Rossum <guido@python.org> | 1998-05-20 16:28:24 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1998-05-20 16:28:24 (GMT) |
commit | 33d7f1a76c3544d2901492cfb6fc9db85f2dfbd6 (patch) | |
tree | 2de3a370c5b0d4a9934d914c1a76e65b14bad8b1 /Lib/random.py | |
parent | 750c8cee7eff436b08bc37e79bc69860c0f743e9 (diff) | |
download | cpython-33d7f1a76c3544d2901492cfb6fc9db85f2dfbd6.zip cpython-33d7f1a76c3544d2901492cfb6fc9db85f2dfbd6.tar.gz cpython-33d7f1a76c3544d2901492cfb6fc9db85f2dfbd6.tar.bz2 |
Add Interfaces to replace remaining needs for importing whrandom.
# XXX TO DO: make the distribution functions below into methods.
Diffstat (limited to 'Lib/random.py')
-rw-r--r-- | Lib/random.py | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/Lib/random.py b/Lib/random.py index 221bef6..d95c324 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -15,9 +15,55 @@ # Translated from anonymously contributed C/C++ source. +import whrandom from whrandom import random, uniform, randint, choice # Also for export! from math import log, exp, pi, e, sqrt, acos, cos, sin +# Interfaces to replace remaining needs for importing whrandom +# XXX TO DO: make the distribution functions below into methods. + +def makeseed(a=None): + """Turn a hashable value into three seed values for whrandom.seed(). + + None or no argument returns (0, 0, 0), to seed from current time. + + """ + if a is None: + return (0, 0, 0) + a = hash(a) + a, x = divmod(a, 256) + a, y = divmod(a, 256) + a, z = divmod(a, 256) + x = (x + a) % 256 or 1 + y = (y + a) % 256 or 1 + z = (z + a) % 256 or 1 + return (x, y, z) + +def seed(a=None): + """Seed the default generator from any hashable value. + + None or no argument returns (0, 0, 0) to seed from current time. + + """ + x, y, z = makeseed(a) + whrandom.seed(x, y, z) + +class generator(whrandom.whrandom): + """Random generator class.""" + + def __init__(self, a=None): + """Constructor. Seed from current time or hashable value.""" + self.seed(a) + + def seed(self, a=None): + """Seed the generator from current time or hashable value.""" + x, y, z = makeseed(a) + whrandom.whrandom.seed(self, x, y, z) + +def new_generator(a=None): + """Return a new random generator instance.""" + return generator(a) + # Housekeeping function to verify that magic constants have been # computed correctly |