]> granicus.if.org Git - python/commitdiff
Add Interfaces to replace remaining needs for importing whrandom.
authorGuido van Rossum <guido@python.org>
Wed, 20 May 1998 16:28:24 +0000 (16:28 +0000)
committerGuido van Rossum <guido@python.org>
Wed, 20 May 1998 16:28:24 +0000 (16:28 +0000)
# XXX TO DO: make the distribution functions below into methods.

Lib/random.py

index 221bef6d9cfa370cd4324fbc5bb01f932bddcfb7..d95c324061714eb0a538b73297c3f52f3569f6d5 100644 (file)
 
 # 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