annotate Lib/whrandom.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents f2f946bd7f47
children 29ced485aca0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # WICHMANN-HILL RANDOM NUMBER GENERATOR
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 # Wichmann, B. A. & Hill, I. D. (1982)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4 # Algorithm AS 183:
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
5 # An efficient and portable pseudo-random number generator
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 # Applied Statistics 31 (1982) 188-190
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 # see also:
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 # Correction to Algorithm AS 183
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 # Applied Statistics 33 (1984) 123
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
12 # McLeod, A. I. (1985)
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 # A remark on Algorithm AS 183
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 # Applied Statistics 34 (1985),198-200
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 #
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 # USE:
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 # whrandom.random() yields double precision random numbers
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 # uniformly distributed between 0 and 1.
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 # Translated by Guido van Rossum from C source provided by
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 # Adrian Baddeley.
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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'
4e1ceaee2963 * Lib/whrandom.py: if seed is (0,0,0), initialize from current
Guido van Rossum <guido@python.org>
parents: 1828
diff changeset
47 if not 0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256:
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)
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1435
diff changeset
57 self._seed = (x, y, z)
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 #
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
59 # Get the next random number in the range [0.0, 1.0).
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 #
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
61 def random(self):
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
62 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
63 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
64 x1, x2 = divmod(x, 177)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
65 y1, y2 = divmod(y, 176)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
66 z1, z2 = divmod(z, 178)
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
67 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
68 x = (171 * x2 - 2 * x1) % 30269
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
69 y = (172 * y2 - 35 * y1) % 30307
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
70 z = (170 * z2 - 63 * z1) % 30323
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
71 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
72 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
73 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
74 return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 #
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
76 # 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
77 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
78 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
79 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
80 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
81 # Get a random integer in the range [a, b] including both end points.
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 #
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
83 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
84 return a + int(self.random() * (b+1-a))
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85 #
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
86 # 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
87 #
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
88 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
89 return seq[int(self.random() * len(seq))]
5
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 # Initialize from the current time
5570dbb1ce55 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 #
1435
560ec7030817 * Mass change: get rid of all init() methods, in favor of __init__()
Guido van Rossum <guido@python.org>
parents: 1178
diff changeset
94 _inst = whrandom()
1078
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
95 seed = _inst.seed
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
96 random = _inst.random
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
97 uniform = _inst.uniform
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
98 randint = _inst.randint
245003c006a6 Misc changes and new modules. whrandom is "objectified". SOCKET.py
Guido van Rossum <guido@python.org>
parents: 5
diff changeset
99 choice = _inst.choice