Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 1 | # 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 Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 21 | # whrandom.seed(x, y, z) must be called before whrandom.random() |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 22 | # to seed the generator |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 23 | # |
| 24 | # There is also an interface to create multiple independent |
| 25 | # random generators, and to choose from other ranges. |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 26 | |
| 27 | |
| 28 | # Translated by Guido van Rossum from C source provided by |
| 29 | # Adrian Baddeley. |
| 30 | |
| 31 | |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 32 | class whrandom: |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 33 | # |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 34 | # Initialize an instance. |
| 35 | # Without arguments, initialize from current time. |
| 36 | # With arguments (x, y, z), initialize from them. |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 37 | # |
Guido van Rossum | 14a6e3d | 1994-09-14 13:33:57 +0000 | [diff] [blame] | 38 | def __init__(self, x = 0, y = 0, z = 0): |
| 39 | self.seed(x, y, z) |
| 40 | # |
| 41 | # Set the seed from (x, y, z). |
| 42 | # These must be integers in the range [0, 256). |
| 43 | # |
| 44 | def seed(self, x = 0, y = 0, z = 0): |
| 45 | if not type(x) == type(y) == type(z) == type(0): |
| 46 | raise TypeError, 'seeds must be integers' |
Guido van Rossum | 6757748 | 1997-01-02 18:13:35 +0000 | [diff] [blame] | 47 | if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256): |
Guido van Rossum | 14a6e3d | 1994-09-14 13:33:57 +0000 | [diff] [blame] | 48 | raise ValueError, 'seeds must be in range(0, 256)' |
| 49 | if 0 == x == y == z: |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 50 | # Initialize from current time |
| 51 | import time |
Guido van Rossum | 9cb018e | 1996-10-21 23:20:03 +0000 | [diff] [blame] | 52 | t = long(time.time() * 256) |
Guido van Rossum | 358473c | 1998-02-16 14:52:42 +0000 | [diff] [blame] | 53 | t = int((t&0xffffff) ^ (t>>24)) |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 54 | t, x = divmod(t, 256) |
| 55 | t, y = divmod(t, 256) |
| 56 | t, z = divmod(t, 256) |
Guido van Rossum | d247812 | 1997-07-10 15:14:50 +0000 | [diff] [blame] | 57 | # Zero is a poor seed, so substitute 1 |
| 58 | self._seed = (x or 1, y or 1, z or 1) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 59 | # |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 60 | # Get the next random number in the range [0.0, 1.0). |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 61 | # |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 62 | def random(self): |
| 63 | x, y, z = self._seed |
| 64 | # |
Guido van Rossum | d247812 | 1997-07-10 15:14:50 +0000 | [diff] [blame] | 65 | x = (171 * x) % 30269 |
| 66 | y = (172 * y) % 30307 |
| 67 | z = (170 * z) % 30323 |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 68 | # |
| 69 | self._seed = x, y, z |
| 70 | # |
| 71 | return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0 |
| 72 | # |
| 73 | # Get a random number in the range [a, b). |
| 74 | # |
| 75 | def uniform(self, a, b): |
| 76 | return a + (b-a) * self.random() |
| 77 | # |
| 78 | # Get a random integer in the range [a, b] including both end points. |
| 79 | # |
| 80 | def randint(self, a, b): |
| 81 | return a + int(self.random() * (b+1-a)) |
| 82 | # |
| 83 | # Choose a random element from a non-empty sequence. |
| 84 | # |
| 85 | def choice(self, seq): |
| 86 | return seq[int(self.random() * len(seq))] |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 87 | |
| 88 | |
| 89 | # Initialize from the current time |
| 90 | # |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 91 | _inst = whrandom() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 92 | seed = _inst.seed |
| 93 | random = _inst.random |
| 94 | uniform = _inst.uniform |
| 95 | randint = _inst.randint |
| 96 | choice = _inst.choice |