blob: 7f8fc60fab18268f1bb359dc6991cb159b99e362 [file] [log] [blame]
Guido van Rossumc6360141990-10-13 19:23:40 +00001# 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#
Guido van Rossum2db91351992-10-18 17:09:59 +000021# whrandom.seed(x, y, z) must be called before whrandom.random()
Guido van Rossumc6360141990-10-13 19:23:40 +000022# to seed the generator
Guido van Rossum2db91351992-10-18 17:09:59 +000023#
24# There is also an interface to create multiple independent
25# random generators, and to choose from other ranges.
Guido van Rossumc6360141990-10-13 19:23:40 +000026
27
28# Translated by Guido van Rossum from C source provided by
29# Adrian Baddeley.
30
31
Guido van Rossumcc20b761998-05-29 17:51:59 +000032# Multi-threading note: the random number generator used here is not
33# thread-safe; it is possible that nearly simultaneous calls in
34# different theads return the same random value. To avoid this, you
35# have to use a lock around all calls. (I didn't want to slow this
36# down in the serial case by using a lock here.)
37
38
Guido van Rossum2db91351992-10-18 17:09:59 +000039class whrandom:
Guido van Rossumc6360141990-10-13 19:23:40 +000040 #
Guido van Rossum2db91351992-10-18 17:09:59 +000041 # Initialize an instance.
42 # Without arguments, initialize from current time.
43 # With arguments (x, y, z), initialize from them.
Guido van Rossumc6360141990-10-13 19:23:40 +000044 #
Guido van Rossum14a6e3d1994-09-14 13:33:57 +000045 def __init__(self, x = 0, y = 0, z = 0):
46 self.seed(x, y, z)
47 #
48 # Set the seed from (x, y, z).
49 # These must be integers in the range [0, 256).
50 #
51 def seed(self, x = 0, y = 0, z = 0):
52 if not type(x) == type(y) == type(z) == type(0):
53 raise TypeError, 'seeds must be integers'
Guido van Rossum67577481997-01-02 18:13:35 +000054 if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
Guido van Rossum14a6e3d1994-09-14 13:33:57 +000055 raise ValueError, 'seeds must be in range(0, 256)'
56 if 0 == x == y == z:
Guido van Rossum2db91351992-10-18 17:09:59 +000057 # Initialize from current time
58 import time
Guido van Rossum9cb018e1996-10-21 23:20:03 +000059 t = long(time.time() * 256)
Guido van Rossum358473c1998-02-16 14:52:42 +000060 t = int((t&0xffffff) ^ (t>>24))
Guido van Rossum2db91351992-10-18 17:09:59 +000061 t, x = divmod(t, 256)
62 t, y = divmod(t, 256)
63 t, z = divmod(t, 256)
Guido van Rossumd2478121997-07-10 15:14:50 +000064 # Zero is a poor seed, so substitute 1
65 self._seed = (x or 1, y or 1, z or 1)
Guido van Rossumc6360141990-10-13 19:23:40 +000066 #
Guido van Rossum2db91351992-10-18 17:09:59 +000067 # Get the next random number in the range [0.0, 1.0).
Guido van Rossumc6360141990-10-13 19:23:40 +000068 #
Guido van Rossum2db91351992-10-18 17:09:59 +000069 def random(self):
Guido van Rossumcc20b761998-05-29 17:51:59 +000070 # This part is thread-unsafe:
71 # BEGIN CRITICAL SECTION
Guido van Rossum2db91351992-10-18 17:09:59 +000072 x, y, z = self._seed
73 #
Guido van Rossumd2478121997-07-10 15:14:50 +000074 x = (171 * x) % 30269
75 y = (172 * y) % 30307
76 z = (170 * z) % 30323
Guido van Rossum2db91351992-10-18 17:09:59 +000077 #
78 self._seed = x, y, z
Guido van Rossumcc20b761998-05-29 17:51:59 +000079 # END CRITICAL SECTION
Guido van Rossum2db91351992-10-18 17:09:59 +000080 #
81 return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
82 #
83 # Get a random number in the range [a, b).
84 #
85 def uniform(self, a, b):
86 return a + (b-a) * self.random()
87 #
88 # Get a random integer in the range [a, b] including both end points.
Guido van Rossum187f1541998-07-31 13:39:44 +000089 # (Deprecated; use randrange below.)
Guido van Rossum2db91351992-10-18 17:09:59 +000090 #
91 def randint(self, a, b):
Guido van Rossum187f1541998-07-31 13:39:44 +000092 return self.randrange(a, b+1)
Guido van Rossum2db91351992-10-18 17:09:59 +000093 #
94 # Choose a random element from a non-empty sequence.
95 #
96 def choice(self, seq):
97 return seq[int(self.random() * len(seq))]
Guido van Rossum187f1541998-07-31 13:39:44 +000098 #
99 # Choose a random item from range([start,] step[, stop]).
100 # This fixes the problem with randint() which includes the
101 # endpoint; in Python this is usually not what you want.
102 #
103 def randrange(self, start, stop=None, step=1,
104 # Do not supply the following arguments
105 int=int, default=None):
106 # This code is a bit messy to make it fast for the
107 # common case while still doing adequate error checking
108 istart = int(start)
109 if istart != start:
110 raise ValueError, "non-integer arg 1 for randrange()"
111 if stop is default:
112 if istart > 0:
113 return int(self.random() * istart)
114 raise ValueError, "empty range for randrange()"
115 istop = int(stop)
116 if istop != stop:
117 raise ValueError, "non-integer stop for randrange()"
118 if step == 1:
119 if istart < istop:
120 return istart + int(self.random() *
121 (istop - istart))
122 raise ValueError, "empty range for randrange()"
123 istep = int(step)
124 if istep != step:
125 raise ValueError, "non-integer step for randrange()"
126 if istep > 0:
127 n = (istop - istart + istep - 1) / istep
128 elif istep < 0:
129 n = (istop - istart + istep + 1) / istep
Guido van Rossumcc221c41998-08-10 20:07:53 +0000130 else:
131 raise ValueError, "zero step for randrange()"
Guido van Rossum187f1541998-07-31 13:39:44 +0000132
Guido van Rossumcc221c41998-08-10 20:07:53 +0000133 if n <= 0:
134 raise ValueError, "empty range for randrange()"
135 return istart + istep*int(self.random() * n)
Guido van Rossumc6360141990-10-13 19:23:40 +0000136
137
138# Initialize from the current time
139#
Guido van Rossum7bc817d1993-12-17 15:25:27 +0000140_inst = whrandom()
Guido van Rossum2db91351992-10-18 17:09:59 +0000141seed = _inst.seed
142random = _inst.random
143uniform = _inst.uniform
144randint = _inst.randint
145choice = _inst.choice
Guido van Rossum187f1541998-07-31 13:39:44 +0000146randrange = _inst.randrange