Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d247812

Browse files
committed
Two improvements suggested by Tim Peters: speed up random() since we
know Python integers are at least 32 bits long; and avoid zeros in initial seed value.
1 parent db25f32 commit d247812

1 file changed

Lines changed: 5 additions & 8 deletions

File tree

Lib/whrandom.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,20 +54,17 @@ def seed(self, x = 0, y = 0, z = 0):
5454
t, x = divmod(t, 256)
5555
t, y = divmod(t, 256)
5656
t, z = divmod(t, 256)
57-
self._seed = (x, y, z)
57+
# Zero is a poor seed, so substitute 1
58+
self._seed = (x or 1, y or 1, z or 1)
5859
#
5960
# Get the next random number in the range [0.0, 1.0).
6061
#
6162
def random(self):
6263
x, y, z = self._seed
6364
#
64-
x1, x2 = divmod(x, 177)
65-
y1, y2 = divmod(y, 176)
66-
z1, z2 = divmod(z, 178)
67-
#
68-
x = (171 * x2 - 2 * x1) % 30269
69-
y = (172 * y2 - 35 * y1) % 30307
70-
z = (170 * z2 - 63 * z1) % 30323
65+
x = (171 * x) % 30269
66+
y = (172 * y) % 30307
67+
z = (170 * z) % 30323
7168
#
7269
self._seed = x, y, z
7370
#

0 commit comments

Comments
 (0)