8000 Add Interfaces to replace remaining needs for importing whrandom. · python/cpython@33d7f1a · GitHub
[go: up one dir, main page]

Skip to content

Commit 33d7f1a

Browse files
committed
Add Interfaces to replace remaining needs for importing whrandom.
# XXX TO DO: make the distribution functions below into methods.
1 parent 750c8ce commit 33d7f1a

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

Lib/random.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,55 @@
1515

1616
# Translated from anonymously contributed C/C++ source.
1717

18+
import whrandom
1819
from whrandom import random, uniform, randint, choice # Also for export!
1920
from math import log, exp, pi, e, sqrt, acos, cos, sin
2021

22+
# Interfaces to replace remaining needs for importing whrandom
23+
# XXX TO DO: make the distribution functions below into methods.
24+
25+
def makeseed(a=None):
26+
"""Turn a hashable value into three seed values for whrandom.seed().
27+
28+
None or no argument returns (0, 0, 0), to seed from current time.
29+
30+
"""
31+
if a is None:
32+
return (0, 0, 0)
33+
a = hash(a)
34+
a, x = divmod(a, 256)
35+
a, y = divmod(a, 256)
36+
a, z = divmod(a, 256)
37+
x = (x + a) % 256 or 1
38+
y = (y + a) % 256 or 1
39+
z = (z + a) % 256 or 1
40+
return (x, y, z)
41+
42+
def seed(a=None):
43+
"""Seed the default generator from any hashable value.
44+
45+
None or no argument returns (0, 0, 0) to seed from current time.
46+
47+
"""
48+
x, y, z = makeseed(a)
49+
whrandom.seed(x, y, z)
50+
51+
class generator(whrandom.whrandom):
52+
"""Random generator class."""
53+
54+
def __init__(self, a=None):
55+
"""Constructor. Seed from current time or hashable value."""
56+
self.seed(a)
57+
58+
def seed(self, a=None):
59+
"""Seed the generator from current time or hashable value."""
60+
x, y, z = makeseed(a)
61+
whrandom.whrandom.seed(self, x, y, z)
62+
63+
def new_generator(a=None):
64+
"""Return a new random generator instance."""
65+
return generator(a)
66+
2167
# Housekeeping function to verify that magic constants have been
2268
# computed correctly
2369

0 commit comments

Comments
 (0)
0