1 # WICHMANN-HILL RANDOM NUMBER GENERATOR
3 # Wichmann, B. A. & Hill, I. D. (1982)
5 # An efficient and portable pseudo-random number generator
6 # Applied Statistics 31 (1982) 188-190
9 # Correction to Algorithm AS 183
10 # Applied Statistics 33 (1984) 123
12 # McLeod, A. I. (1985)
13 # A remark on Algorithm AS 183
14 # Applied Statistics 34 (1985),198-200
18 # whrandom.random() yields double precision random numbers
19 # uniformly distributed between 0 and 1.
21 # whrandom.seed(x, y, z) must be called before whrandom.random()
22 # to seed the generator
24 # There is also an interface to create multiple independent
25 # random generators, and to choose from other ranges.
28 # Translated by Guido van Rossum from C source provided by
32 # 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.)
41 # Initialize an instance.
42 # Without arguments, initialize from current time.
43 # With arguments (x, y, z), initialize from them.
45 def __init__(self
, x
= 0, y
= 0, z
= 0):
48 # Set the seed from (x, y, z).
49 # These must be integers in the range [0, 256).
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'
54 if not (0 <= x
< 256 and 0 <= y
< 256 and 0 <= z
< 256):
55 raise ValueError, 'seeds must be in range(0, 256)'
57 # Initialize from current time
59 t
= long(time
.time() * 256)
60 t
= int((t
&0xffffff) ^
(t
>>24))
64 # Zero is a poor seed, so substitute 1
65 self
._seed
= (x
or 1, y
or 1, z
or 1)
67 # Get the next random number in the range [0.0, 1.0).
70 # This part is thread-unsafe:
71 # BEGIN CRITICAL SECTION
79 # END CRITICAL SECTION
81 return (x
/30269.0 + y
/30307.0 + z
/30323.0) % 1.0
83 # Get a random number in the range [a, b).
85 def uniform(self
, a
, b
):
86 return a
+ (b
-a
) * self
.random()
88 # Get a random integer in the range [a, b] including both end points.
89 # (Deprecated; use randrange below.)
91 def randint(self
, a
, b
):
92 return self
.randrange(a
, b
+1)
94 # Choose a random element from a non-empty sequence.
96 def choice(self
, seq
):
97 return seq
[int(self
.random() * len(seq
))]
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.
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
110 raise ValueError, "non-integer arg 1 for randrange()"
113 return int(self
.random() * istart
)
114 raise ValueError, "empty range for randrange()"
117 raise ValueError, "non-integer stop for randrange()"
120 return istart
+ int(self
.random() *
122 raise ValueError, "empty range for randrange()"
125 raise ValueError, "non-integer step for randrange()"
127 n
= (istop
- istart
+ istep
- 1) / istep
129 n
= (istop
- istart
+ istep
+ 1) / istep
131 raise ValueError, "zero step for randrange()"
134 raise ValueError, "empty range for randrange()"
135 return istart
+ istep
*int(self
.random() * n
)
138 # Initialize from the current time
142 random
= _inst
.random
143 uniform
= _inst
.uniform
144 randint
= _inst
.randint
145 choice
= _inst
.choice
146 randrange
= _inst
.randrange