blob: c7881b551b5a9d16234d402339fa21f2fb444291 [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 Rossum2db91351992-10-18 17:09:59 +000032class whrandom:
Guido van Rossumc6360141990-10-13 19:23:40 +000033 #
Guido van Rossum2db91351992-10-18 17:09:59 +000034 # Initialize an instance.
35 # Without arguments, initialize from current time.
36 # With arguments (x, y, z), initialize from them.
Guido van Rossumc6360141990-10-13 19:23:40 +000037 #
Guido van Rossum7bc817d1993-12-17 15:25:27 +000038 def __init__(self, *xyz):
Guido van Rossum2db91351992-10-18 17:09:59 +000039 if not xyz:
40 # Initialize from current time
41 import time
Guido van Rossumfea2af11993-01-04 09:16:51 +000042 t = int(time.time())
Guido van Rossum2db91351992-10-18 17:09:59 +000043 t, x = divmod(t, 256)
44 t, y = divmod(t, 256)
45 t, z = divmod(t, 256)
46 else:
47 # Initialize from arguments (x, y, z)
48 x, y, z = xyz
49 self.seed(x, y, z)
Guido van Rossumc6360141990-10-13 19:23:40 +000050 #
Guido van Rossum2db91351992-10-18 17:09:59 +000051 # Set the seed from (x, y, z).
52 # These must be integers in the range [0, 256).
Guido van Rossumc6360141990-10-13 19:23:40 +000053 #
Guido van Rossum2db91351992-10-18 17:09:59 +000054 def seed(self, *xyz):
55 if type(xyz) <> type(()) or len(xyz) <> 3:
56 raise TypeError, '3 seeds required'
57 x, y, z = xyz
58 if not type(x) == type(y) == type(z) == type(0):
59 raise TypeError, 'seeds must be integers'
60 if not 0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256:
61 raise ValueError, 'seeds must be in range(0, 256)'
62 self._seed = xyz
Guido van Rossumc6360141990-10-13 19:23:40 +000063 #
Guido van Rossum2db91351992-10-18 17:09:59 +000064 # Get the next random number in the range [0.0, 1.0).
Guido van Rossumc6360141990-10-13 19:23:40 +000065 #
Guido van Rossum2db91351992-10-18 17:09:59 +000066 def random(self):
67 x, y, z = self._seed
68 #
69 x1, x2 = divmod(x, 177)
70 y1, y2 = divmod(y, 176)
71 z1, z2 = divmod(z, 178)
72 #
73 x = (171 * x2 - 2 * x1) % 30269
74 y = (172 * y2 - 35 * y1) % 30307
75 z = (170 * z2 - 63 * z1) % 30323
76 #
77 self._seed = x, y, z
78 #
79 return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
80 #
81 # Get a random number in the range [a, b).
82 #
83 def uniform(self, a, b):
84 return a + (b-a) * self.random()
85 #
86 # Get a random integer in the range [a, b] including both end points.
87 #
88 def randint(self, a, b):
89 return a + int(self.random() * (b+1-a))
90 #
91 # Choose a random element from a non-empty sequence.
92 #
93 def choice(self, seq):
94 return seq[int(self.random() * len(seq))]
Guido van Rossumc6360141990-10-13 19:23:40 +000095
96
97# Initialize from the current time
98#
Guido van Rossum7bc817d1993-12-17 15:25:27 +000099_inst = whrandom()
Guido van Rossum2db91351992-10-18 17:09:59 +0000100seed = _inst.seed
101random = _inst.random
102uniform = _inst.uniform
103randint = _inst.randint
104choice = _inst.choice