Kaydet (Commit) d2478125 authored tarafından Guido van Rossum's avatar Guido van Rossum

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.
üst db25f328
...@@ -54,20 +54,17 @@ class whrandom: ...@@ -54,20 +54,17 @@ class whrandom:
t, x = divmod(t, 256) t, x = divmod(t, 256)
t, y = divmod(t, 256) t, y = divmod(t, 256)
t, z = divmod(t, 256) t, z = divmod(t, 256)
self._seed = (x, y, z) # Zero is a poor seed, so substitute 1
self._seed = (x or 1, y or 1, z or 1)
# #
# Get the next random number in the range [0.0, 1.0). # Get the next random number in the range [0.0, 1.0).
# #
def random(self): def random(self):
x, y, z = self._seed x, y, z = self._seed
# #
x1, x2 = divmod(x, 177) x = (171 * x) % 30269
y1, y2 = divmod(y, 176) y = (172 * y) % 30307
z1, z2 = divmod(z, 178) z = (170 * z) % 30323
#
x = (171 * x2 - 2 * x1) % 30269
y = (172 * y2 - 35 * y1) % 30307
z = (170 * z2 - 63 * z1) % 30323
# #
self._seed = x, y, z self._seed = x, y, z
# #
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment