blob: bc0d1a4520eac209807d29e79eac9d329b82c3cb [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""Wichman-Hill random number generator.
2
3Wichmann, B. A. & Hill, I. D. (1982)
Tim Peterse1190062001-01-15 03:34:38 +00004Algorithm AS 183:
Guido van Rossume7b146f2000-02-04 15:28:42 +00005An efficient and portable pseudo-random number generator
6Applied Statistics 31 (1982) 188-190
7
Tim Peterse1190062001-01-15 03:34:38 +00008see also:
Guido van Rossume7b146f2000-02-04 15:28:42 +00009 Correction to Algorithm AS 183
Tim Peterse1190062001-01-15 03:34:38 +000010 Applied Statistics 33 (1984) 123
Guido van Rossume7b146f2000-02-04 15:28:42 +000011
12 McLeod, A. I. (1985)
Tim Peterse1190062001-01-15 03:34:38 +000013 A remark on Algorithm AS 183
Guido van Rossume7b146f2000-02-04 15:28:42 +000014 Applied Statistics 34 (1985),198-200
Guido van Rossumc6360141990-10-13 19:23:40 +000015
16
Guido van Rossume7b146f2000-02-04 15:28:42 +000017USE:
Tim Peterse1190062001-01-15 03:34:38 +000018whrandom.random() yields double precision random numbers
Guido van Rossume7b146f2000-02-04 15:28:42 +000019 uniformly distributed between 0 and 1.
20
21whrandom.seed(x, y, z) must be called before whrandom.random()
22 to seed the generator
23
24There is also an interface to create multiple independent
25random generators, and to choose from other ranges.
Guido van Rossumc6360141990-10-13 19:23:40 +000026
27
Guido van Rossume7b146f2000-02-04 15:28:42 +000028
29Multi-threading note: the random number generator used here is not
30thread-safe; it is possible that nearly simultaneous calls in
31different theads return the same random value. To avoid this, you
32have to use a lock around all calls. (I didn't want to slow this
33down in the serial case by using a lock here.)
34"""
35
Andrew M. Kuchling60395122004-08-07 21:44:37 +000036import warnings
37warnings.warn("the whrandom module is deprecated; please use the random module",
38 DeprecationWarning)
39
Guido van Rossum98d9fd32000-02-28 15:12:25 +000040# Translated by Guido van Rossum from C source provided by
41# Adrian Baddeley.
42
43
Guido van Rossum2db91351992-10-18 17:09:59 +000044class whrandom:
Tim Peterse1190062001-01-15 03:34:38 +000045 def __init__(self, x = 0, y = 0, z = 0):
46 """Initialize an instance.
47 Without arguments, initialize from current time.
48 With arguments (x, y, z), initialize from them."""
49 self.seed(x, y, z)
Guido van Rossume7b146f2000-02-04 15:28:42 +000050
Tim Peterse1190062001-01-15 03:34:38 +000051 def seed(self, x = 0, y = 0, z = 0):
52 """Set the seed from (x, y, z).
53 These must be integers in the range [0, 256)."""
54 if not type(x) == type(y) == type(z) == type(0):
55 raise TypeError, 'seeds must be integers'
56 if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
57 raise ValueError, 'seeds must be in range(0, 256)'
58 if 0 == x == y == z:
59 # Initialize from current time
60 import time
61 t = long(time.time() * 256)
62 t = int((t&0xffffff) ^ (t>>24))
63 t, x = divmod(t, 256)
64 t, y = divmod(t, 256)
65 t, z = divmod(t, 256)
66 # Zero is a poor seed, so substitute 1
67 self._seed = (x or 1, y or 1, z or 1)
Guido van Rossume7b146f2000-02-04 15:28:42 +000068
Tim Peterse1190062001-01-15 03:34:38 +000069 def random(self):
70 """Get the next random number in the range [0.0, 1.0)."""
71 # This part is thread-unsafe:
72 # BEGIN CRITICAL SECTION
73 x, y, z = self._seed
74 #
75 x = (171 * x) % 30269
76 y = (172 * y) % 30307
77 z = (170 * z) % 30323
78 #
79 self._seed = x, y, z
80 # END CRITICAL SECTION
81 #
82 return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
Guido van Rossume7b146f2000-02-04 15:28:42 +000083
Tim Peterse1190062001-01-15 03:34:38 +000084 def uniform(self, a, b):
85 """Get a random number in the range [a, b)."""
86 return a + (b-a) * self.random()
Guido van Rossume7b146f2000-02-04 15:28:42 +000087
Tim Peterse1190062001-01-15 03:34:38 +000088 def randint(self, a, b):
89 """Get a random integer in the range [a, b] including
90 both end points.
Fred Drake0412e492000-06-30 19:33:35 +000091
Tim Peterse1190062001-01-15 03:34:38 +000092 (Deprecated; use randrange below.)"""
93 return self.randrange(a, b+1)
Guido van Rossume7b146f2000-02-04 15:28:42 +000094
Tim Peterse1190062001-01-15 03:34:38 +000095 def choice(self, seq):
96 """Choose a random element from a non-empty sequence."""
97 return seq[int(self.random() * len(seq))]
Guido van Rossume7b146f2000-02-04 15:28:42 +000098
Tim Peterse1190062001-01-15 03:34:38 +000099 def randrange(self, start, stop=None, step=1, int=int, default=None):
100 """Choose a random item from range(start, stop[, step]).
Fred Drake0412e492000-06-30 19:33:35 +0000101
Tim Peterse1190062001-01-15 03:34:38 +0000102 This fixes the problem with randint() which includes the
103 endpoint; in Python this is usually not what you want.
104 Do not supply the 'int' and 'default' arguments."""
105 # This code is a bit messy to make it fast for the
106 # common case while still doing adequate error checking
107 istart = int(start)
108 if istart != start:
109 raise ValueError, "non-integer arg 1 for randrange()"
110 if stop is default:
111 if istart > 0:
112 return int(self.random() * istart)
113 raise ValueError, "empty range for randrange()"
114 istop = int(stop)
115 if istop != stop:
116 raise ValueError, "non-integer stop for randrange()"
117 if step == 1:
118 if istart < istop:
119 return istart + int(self.random() *
120 (istop - istart))
121 raise ValueError, "empty range for randrange()"
122 istep = int(step)
123 if istep != step:
124 raise ValueError, "non-integer step for randrange()"
125 if istep > 0:
126 n = (istop - istart + istep - 1) / istep
127 elif istep < 0:
128 n = (istop - istart + istep + 1) / istep
129 else:
130 raise ValueError, "zero step for randrange()"
Guido van Rossum187f1541998-07-31 13:39:44 +0000131
Tim Peterse1190062001-01-15 03:34:38 +0000132 if n <= 0:
133 raise ValueError, "empty range for randrange()"
134 return istart + istep*int(self.random() * n)
Guido van Rossumc6360141990-10-13 19:23:40 +0000135
136
137# Initialize from the current time
Guido van Rossum7bc817d1993-12-17 15:25:27 +0000138_inst = whrandom()
Guido van Rossum2db91351992-10-18 17:09:59 +0000139seed = _inst.seed
140random = _inst.random
141uniform = _inst.uniform
142randint = _inst.randint
143choice = _inst.choice
Guido van Rossum187f1541998-07-31 13:39:44 +0000144randrange = _inst.randrange