Mercurial > cpython-lru_cache-weakref
annotate Lib/whrandom.py @ 5817:48b94de85bf3 legacy-trunk
Extensive changes by AMK.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Thu, 17 Jul 1997 16:29:42 +0000 |
| parents | 4b42eea10a02 |
| children | 48df2080d7d4 |
| rev | line source |
|---|---|
| 5 | 1 # WICHMANN-HILL RANDOM NUMBER GENERATOR |
| 2 # | |
| 3 # Wichmann, B. A. & Hill, I. D. (1982) | |
| 4 # Algorithm AS 183: | |
| 5 # An efficient and portable pseudo-random number generator | |
| 6 # Applied Statistics 31 (1982) 188-190 | |
| 7 # | |
| 8 # see also: | |
| 9 # Correction to Algorithm AS 183 | |
| 10 # Applied Statistics 33 (1984) 123 | |
| 11 # | |
| 12 # McLeod, A. I. (1985) | |
| 13 # A remark on Algorithm AS 183 | |
| 14 # Applied Statistics 34 (1985),198-200 | |
| 15 # | |
| 16 # | |
| 17 # USE: | |
| 18 # whrandom.random() yields double precision random numbers | |
| 19 # uniformly distributed between 0 and 1. | |
| 20 # | |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
21 # whrandom.seed(x, y, z) must be called before whrandom.random() |
| 5 | 22 # to seed the generator |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
23 # |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
24 # There is also an interface to create multiple independent |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
25 # random generators, and to choose from other ranges. |
| 5 | 26 |
| 27 | |
| 28 # Translated by Guido van Rossum from C source provided by | |
| 29 # Adrian Baddeley. | |
| 30 | |
| 31 | |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
32 class whrandom: |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
33 # |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
34 # Initialize an instance. |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
35 # Without arguments, initialize from current time. |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
36 # With arguments (x, y, z), initialize from them. |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
37 # |
|
1889
4e1ceaee2963
* Lib/whrandom.py: if seed is (0,0,0), initialize from current
Guido van Rossum <guido@python.org>
parents:
1828
diff
changeset
|
38 def __init__(self, x = 0, y = 0, z = 0): |
|
4e1ceaee2963
* Lib/whrandom.py: if seed is (0,0,0), initialize from current
Guido van Rossum <guido@python.org>
parents:
1828
diff
changeset
|
39 self.seed(x, y, z) |
|
4e1ceaee2963
* Lib/whrandom.py: if seed is (0,0,0), initialize from current
Guido van Rossum <guido@python.org>
parents:
1828
diff
changeset
|
40 # |
|
4e1ceaee2963
* Lib/whrandom.py: if seed is (0,0,0), initialize from current
Guido van Rossum <guido@python.org>
parents:
1828
diff
changeset
|
41 # Set the seed from (x, y, z). |
|
4e1ceaee2963
* Lib/whrandom.py: if seed is (0,0,0), initialize from current
Guido van Rossum <guido@python.org>
parents:
1828
diff
changeset
|
42 # These must be integers in the range [0, 256). |
|
4e1ceaee2963
* Lib/whrandom.py: if seed is (0,0,0), initialize from current
Guido van Rossum <guido@python.org>
parents:
1828
diff
changeset
|
43 # |
|
4e1ceaee2963
* Lib/whrandom.py: if seed is (0,0,0), initialize from current
Guido van Rossum <guido@python.org>
parents:
1828
diff
changeset
|
44 def seed(self, x = 0, y = 0, z = 0): |
|
4e1ceaee2963
* Lib/whrandom.py: if seed is (0,0,0), initialize from current
Guido van Rossum <guido@python.org>
parents:
1828
diff
changeset
|
45 if not type(x) == type(y) == type(z) == type(0): |
|
4e1ceaee2963
* Lib/whrandom.py: if seed is (0,0,0), initialize from current
Guido van Rossum <guido@python.org>
parents:
1828
diff
changeset
|
46 raise TypeError, 'seeds must be integers' |
|
4852
29ced485aca0
Properly parenthesize a long Boolean combination. Formerly, you could
Guido van Rossum <guido@python.org>
parents:
4431
diff
changeset
|
47 if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256): |
|
1889
4e1ceaee2963
* Lib/whrandom.py: if seed is (0,0,0), initialize from current
Guido van Rossum <guido@python.org>
parents:
1828
diff
changeset
|
48 raise ValueError, 'seeds must be in range(0, 256)' |
|
4e1ceaee2963
* Lib/whrandom.py: if seed is (0,0,0), initialize from current
Guido van Rossum <guido@python.org>
parents:
1828
diff
changeset
|
49 if 0 == x == y == z: |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
50 # Initialize from current time |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
51 import time |
|
4431
f2f946bd7f47
Change the default seeding -- use 8 bits of sub-second precision and
Guido van Rossum <guido@python.org>
parents:
1889
diff
changeset
|
52 t = long(time.time() * 256) |
|
f2f946bd7f47
Change the default seeding -- use 8 bits of sub-second precision and
Guido van Rossum <guido@python.org>
parents:
1889
diff
changeset
|
53 t = int((t&0xffffff) | (t>>24)) |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
54 t, x = divmod(t, 256) |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
55 t, y = divmod(t, 256) |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
56 t, z = divmod(t, 256) |
|
5756
4b42eea10a02
Two improvements suggested by Tim Peters: speed up random() since we
Guido van Rossum <guido@python.org>
parents:
4852
diff
changeset
|
57 # Zero is a poor seed, so substitute 1 |
|
4b42eea10a02
Two improvements suggested by Tim Peters: speed up random() since we
Guido van Rossum <guido@python.org>
parents:
4852
diff
changeset
|
58 self._seed = (x or 1, y or 1, z or 1) |
| 5 | 59 # |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
60 # Get the next random number in the range [0.0, 1.0). |
| 5 | 61 # |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
62 def random(self): |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
63 x, y, z = self._seed |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
64 # |
|
5756
4b42eea10a02
Two improvements suggested by Tim Peters: speed up random() since we
Guido van Rossum <guido@python.org>
parents:
4852
diff
changeset
|
65 x = (171 * x) % 30269 |
|
4b42eea10a02
Two improvements suggested by Tim Peters: speed up random() since we
Guido van Rossum <guido@python.org>
parents:
4852
diff
changeset
|
66 y = (172 * y) % 30307 |
|
4b42eea10a02
Two improvements suggested by Tim Peters: speed up random() since we
Guido van Rossum <guido@python.org>
parents:
4852
diff
changeset
|
67 z = (170 * z) % 30323 |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
68 # |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
69 self._seed = x, y, z |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
70 # |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
71 return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0 |
| 5 | 72 # |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
73 # Get a random number in the range [a, b). |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
74 # |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
75 def uniform(self, a, b): |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
76 return a + (b-a) * self.random() |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
77 # |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
78 # Get a random integer in the range [a, b] including both end points. |
| 5 | 79 # |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
80 def randint(self, a, b): |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
81 return a + int(self.random() * (b+1-a)) |
| 5 | 82 # |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
83 # Choose a random element from a non-empty sequence. |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
84 # |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
85 def choice(self, seq): |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
86 return seq[int(self.random() * len(seq))] |
| 5 | 87 |
| 88 | |
| 89 # Initialize from the current time | |
| 90 # | |
|
1435
560ec7030817
* Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents:
1178
diff
changeset
|
91 _inst = whrandom() |
|
1078
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
92 seed = _inst.seed |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
93 random = _inst.random |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
94 uniform = _inst.uniform |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
95 randint = _inst.randint |
|
245003c006a6
Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents:
5
diff
changeset
|
96 choice = _inst.choice |
