Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Wichman-Hill random number generator. |
| 2 | |
| 3 | Wichmann, B. A. & Hill, I. D. (1982) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 4 | Algorithm AS 183: |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 5 | An efficient and portable pseudo-random number generator |
| 6 | Applied Statistics 31 (1982) 188-190 |
| 7 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 8 | see also: |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 9 | Correction to Algorithm AS 183 |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 10 | Applied Statistics 33 (1984) 123 |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 11 | |
| 12 | McLeod, A. I. (1985) |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 13 | A remark on Algorithm AS 183 |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 14 | Applied Statistics 34 (1985),198-200 |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 15 | |
| 16 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 17 | USE: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 18 | whrandom.random() yields double precision random numbers |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 19 | uniformly distributed between 0 and 1. |
| 20 | |
| 21 | whrandom.seed(x, y, z) must be called before whrandom.random() |
| 22 | to seed the generator |
| 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 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 28 | |
| 29 | Multi-threading note: the random number generator used here is not |
| 30 | thread-safe; it is possible that nearly simultaneous calls in |
| 31 | different theads return the same random value. To avoid this, you |
| 32 | have to use a lock around all calls. (I didn't want to slow this |
| 33 | down in the serial case by using a lock here.) |
| 34 | """ |
| 35 | |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 36 | # Translated by Guido van Rossum from C source provided by |
| 37 | # Adrian Baddeley. |
| 38 | |
Fred Drake | 6e99704 | 2002-04-10 01:45:11 +0000 | [diff] [blame^] | 39 | import warnings |
| 40 | warnings.warn("the whrandom module is deprecated; please use random", |
| 41 | DeprecationWarning) |
| 42 | del warnings |
| 43 | |
Guido van Rossum | 98d9fd3 | 2000-02-28 15:12:25 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 45 | class whrandom: |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 46 | def __init__(self, x = 0, y = 0, z = 0): |
| 47 | """Initialize an instance. |
| 48 | Without arguments, initialize from current time. |
| 49 | With arguments (x, y, z), initialize from them.""" |
| 50 | self.seed(x, y, z) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 51 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 52 | def seed(self, x = 0, y = 0, z = 0): |
| 53 | """Set the seed from (x, y, z). |
| 54 | These must be integers in the range [0, 256).""" |
| 55 | if not type(x) == type(y) == type(z) == type(0): |
| 56 | raise TypeError, 'seeds must be integers' |
| 57 | if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256): |
| 58 | raise ValueError, 'seeds must be in range(0, 256)' |
| 59 | if 0 == x == y == z: |
| 60 | # Initialize from current time |
| 61 | import time |
| 62 | t = long(time.time() * 256) |
| 63 | t = int((t&0xffffff) ^ (t>>24)) |
| 64 | t, x = divmod(t, 256) |
| 65 | t, y = divmod(t, 256) |
| 66 | t, z = divmod(t, 256) |
| 67 | # Zero is a poor seed, so substitute 1 |
| 68 | self._seed = (x or 1, y or 1, z or 1) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 69 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 70 | def random(self): |
| 71 | """Get the next random number in the range [0.0, 1.0).""" |
| 72 | # This part is thread-unsafe: |
| 73 | # BEGIN CRITICAL SECTION |
| 74 | x, y, z = self._seed |
| 75 | # |
| 76 | x = (171 * x) % 30269 |
| 77 | y = (172 * y) % 30307 |
| 78 | z = (170 * z) % 30323 |
| 79 | # |
| 80 | self._seed = x, y, z |
| 81 | # END CRITICAL SECTION |
| 82 | # |
| 83 | return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0 |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 84 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 85 | def uniform(self, a, b): |
| 86 | """Get a random number in the range [a, b).""" |
| 87 | return a + (b-a) * self.random() |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 88 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 89 | def randint(self, a, b): |
| 90 | """Get a random integer in the range [a, b] including |
| 91 | both end points. |
Fred Drake | 0412e49 | 2000-06-30 19:33:35 +0000 | [diff] [blame] | 92 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 93 | (Deprecated; use randrange below.)""" |
| 94 | return self.randrange(a, b+1) |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 95 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 96 | def choice(self, seq): |
| 97 | """Choose a random element from a non-empty sequence.""" |
| 98 | return seq[int(self.random() * len(seq))] |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 99 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 100 | def randrange(self, start, stop=None, step=1, int=int, default=None): |
| 101 | """Choose a random item from range(start, stop[, step]). |
Fred Drake | 0412e49 | 2000-06-30 19:33:35 +0000 | [diff] [blame] | 102 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 103 | This fixes the problem with randint() which includes the |
| 104 | endpoint; in Python this is usually not what you want. |
| 105 | Do not supply the 'int' and 'default' arguments.""" |
| 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 |
| 130 | else: |
| 131 | raise ValueError, "zero step for randrange()" |
Guido van Rossum | 187f154 | 1998-07-31 13:39:44 +0000 | [diff] [blame] | 132 | |
Tim Peters | e119006 | 2001-01-15 03:34:38 +0000 | [diff] [blame] | 133 | if n <= 0: |
| 134 | raise ValueError, "empty range for randrange()" |
| 135 | return istart + istep*int(self.random() * n) |
Guido van Rossum | c636014 | 1990-10-13 19:23:40 +0000 | [diff] [blame] | 136 | |
| 137 | |
| 138 | # Initialize from the current time |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 139 | _inst = whrandom() |
Guido van Rossum | 2db9135 | 1992-10-18 17:09:59 +0000 | [diff] [blame] | 140 | seed = _inst.seed |
| 141 | random = _inst.random |
| 142 | uniform = _inst.uniform |
| 143 | randint = _inst.randint |
| 144 | choice = _inst.choice |
Guido van Rossum | 187f154 | 1998-07-31 13:39:44 +0000 | [diff] [blame] | 145 | randrange = _inst.randrange |