Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 1 | """Random variable generators. |
Guido van Rossum | ff03b1a | 1994-03-09 12:55:02 +0000 | [diff] [blame] | 2 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 3 | integers |
| 4 | -------- |
| 5 | uniform within range |
| 6 | |
| 7 | sequences |
| 8 | --------- |
| 9 | pick random element |
Raymond Hettinger | f24eb35 | 2002-11-12 17:41:57 +0000 | [diff] [blame] | 10 | pick random sample |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 11 | generate random permutation |
| 12 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 13 | distributions on the real line: |
| 14 | ------------------------------ |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 15 | uniform |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 16 | normal (Gaussian) |
| 17 | lognormal |
| 18 | negative exponential |
| 19 | gamma |
| 20 | beta |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 21 | pareto |
| 22 | Weibull |
Guido van Rossum | ff03b1a | 1994-03-09 12:55:02 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 24 | distributions on the circle (angles 0 to 2pi) |
| 25 | --------------------------------------------- |
| 26 | circular uniform |
| 27 | von Mises |
| 28 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 29 | General notes on the underlying Mersenne Twister core generator: |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 30 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 31 | * The period is 2**19937-1. |
Tim Peters | 0e11595 | 2006-06-10 22:51:45 +0000 | [diff] [blame] | 32 | * It is one of the most extensively tested generators in existence. |
| 33 | * Without a direct way to compute N steps forward, the semantics of |
| 34 | jumpahead(n) are weakened to simply jump to another distant state and rely |
| 35 | on the large period to avoid overlapping sequences. |
| 36 | * The random() method is implemented in C, executes in a single Python step, |
| 37 | and is, therefore, threadsafe. |
Tim Peters | e360d95 | 2001-01-26 10:00:39 +0000 | [diff] [blame] | 38 | |
Guido van Rossum | e7b146f | 2000-02-04 15:28:42 +0000 | [diff] [blame] | 39 | """ |
Guido van Rossum | d03e119 | 1998-05-29 17:51:31 +0000 | [diff] [blame] | 40 | |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 41 | from warnings import warn as _warn |
| 42 | from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType |
Raymond Hettinger | 91e27c2 | 2005-08-19 01:36:35 +0000 | [diff] [blame] | 43 | from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 44 | from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin |
Raymond Hettinger | c1c43ca | 2004-09-05 00:00:42 +0000 | [diff] [blame] | 45 | from os import urandom as _urandom |
| 46 | from binascii import hexlify as _hexlify |
Guido van Rossum | ff03b1a | 1994-03-09 12:55:02 +0000 | [diff] [blame] | 47 | |
Raymond Hettinger | f24eb35 | 2002-11-12 17:41:57 +0000 | [diff] [blame] | 48 | __all__ = ["Random","seed","random","uniform","randint","choice","sample", |
Skip Montanaro | 0de6580 | 2001-02-15 22:15:14 +0000 | [diff] [blame] | 49 | "randrange","shuffle","normalvariate","lognormvariate", |
Raymond Hettinger | f8a52d3 | 2003-08-05 12:23:19 +0000 | [diff] [blame] | 50 | "expovariate","vonmisesvariate","gammavariate", |
| 51 | "gauss","betavariate","paretovariate","weibullvariate", |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 52 | "getstate","setstate","jumpahead", "WichmannHill", "getrandbits", |
Raymond Hettinger | 23f1241 | 2004-09-13 22:23:21 +0000 | [diff] [blame] | 53 | "SystemRandom"] |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 54 | |
| 55 | NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0) |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 56 | TWOPI = 2.0*_pi |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 57 | LOG4 = _log(4.0) |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 58 | SG_MAGICCONST = 1.0 + _log(4.5) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 59 | BPF = 53 # Number of bits in a float |
Tim Peters | 7c2a85b | 2004-08-31 02:19:55 +0000 | [diff] [blame] | 60 | RECIP_BPF = 2**-BPF |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 61 | |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 62 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 63 | # Translated by Guido van Rossum from C source provided by |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 64 | # Adrian Baddeley. Adapted by Raymond Hettinger for use with |
Raymond Hettinger | 3fa19d7 | 2004-08-31 01:05:15 +0000 | [diff] [blame] | 65 | # the Mersenne Twister and os.urandom() core generators. |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 66 | |
Raymond Hettinger | 145a4a0 | 2003-01-07 10:25:55 +0000 | [diff] [blame] | 67 | import _random |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 68 | |
Raymond Hettinger | 145a4a0 | 2003-01-07 10:25:55 +0000 | [diff] [blame] | 69 | class Random(_random.Random): |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 70 | """Random number generator base class used by bound module functions. |
| 71 | |
| 72 | Used to instantiate instances of Random to get generators that don't |
| 73 | share state. Especially useful for multi-threaded programs, creating |
| 74 | a different instance of Random for each thread, and using the jumpahead() |
| 75 | method to ensure that the generated sequences seen by each thread don't |
| 76 | overlap. |
| 77 | |
| 78 | Class Random can also be subclassed if you want to use a different basic |
| 79 | generator of your own devising: in that case, override the following |
| 80 | methods: random(), seed(), getstate(), setstate() and jumpahead(). |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 81 | Optionally, implement a getrandombits() method so that randrange() |
| 82 | can cover arbitrarily large ranges. |
Raymond Hettinger | ef4d4bd | 2002-05-23 23:58:17 +0000 | [diff] [blame] | 83 | |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 84 | """ |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 85 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 86 | VERSION = 2 # used by getstate/setstate |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 87 | |
| 88 | def __init__(self, x=None): |
| 89 | """Initialize an instance. |
| 90 | |
| 91 | Optional argument x controls seeding, as for Random.seed(). |
| 92 | """ |
| 93 | |
| 94 | self.seed(x) |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 95 | self.gauss_next = None |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 96 | |
Tim Peters | 0de88fc | 2001-02-01 04:59:18 +0000 | [diff] [blame] | 97 | def seed(self, a=None): |
| 98 | """Initialize internal state from hashable object. |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 99 | |
Raymond Hettinger | 23f1241 | 2004-09-13 22:23:21 +0000 | [diff] [blame] | 100 | None or no argument seeds from current time or from an operating |
| 101 | system specific randomness source if available. |
Tim Peters | 0de88fc | 2001-02-01 04:59:18 +0000 | [diff] [blame] | 102 | |
Tim Peters | bcd725f | 2001-02-01 10:06:53 +0000 | [diff] [blame] | 103 | If a is not None or an int or long, hash(a) is used instead. |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 104 | """ |
| 105 | |
Raymond Hettinger | 3081d59 | 2003-08-09 18:30:57 +0000 | [diff] [blame] | 106 | if a is None: |
Raymond Hettinger | c1c43ca | 2004-09-05 00:00:42 +0000 | [diff] [blame] | 107 | try: |
| 108 | a = long(_hexlify(_urandom(16)), 16) |
| 109 | except NotImplementedError: |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 110 | import time |
| 111 | a = long(time.time() * 256) # use fractional seconds |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 112 | |
Raymond Hettinger | 145a4a0 | 2003-01-07 10:25:55 +0000 | [diff] [blame] | 113 | super(Random, self).seed(a) |
Tim Peters | 46c04e1 | 2002-05-05 20:40:00 +0000 | [diff] [blame] | 114 | self.gauss_next = None |
| 115 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 116 | def getstate(self): |
| 117 | """Return internal state; can be passed to setstate() later.""" |
Raymond Hettinger | 145a4a0 | 2003-01-07 10:25:55 +0000 | [diff] [blame] | 118 | return self.VERSION, super(Random, self).getstate(), self.gauss_next |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 119 | |
| 120 | def setstate(self, state): |
| 121 | """Restore internal state from object returned by getstate().""" |
| 122 | version = state[0] |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 123 | if version == 2: |
| 124 | version, internalstate, self.gauss_next = state |
Raymond Hettinger | 145a4a0 | 2003-01-07 10:25:55 +0000 | [diff] [blame] | 125 | super(Random, self).setstate(internalstate) |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 126 | else: |
| 127 | raise ValueError("state with version %s passed to " |
| 128 | "Random.setstate() of version %s" % |
| 129 | (version, self.VERSION)) |
| 130 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 131 | ## ---- Methods below this point do not need to be overridden when |
| 132 | ## ---- subclassing for the purpose of using a different core generator. |
| 133 | |
| 134 | ## -------------------- pickle support ------------------- |
| 135 | |
| 136 | def __getstate__(self): # for pickle |
| 137 | return self.getstate() |
| 138 | |
| 139 | def __setstate__(self, state): # for pickle |
| 140 | self.setstate(state) |
| 141 | |
Raymond Hettinger | 5f078ff | 2003-06-24 20:29:04 +0000 | [diff] [blame] | 142 | def __reduce__(self): |
| 143 | return self.__class__, (), self.getstate() |
| 144 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 145 | ## -------------------- integer methods ------------------- |
| 146 | |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 147 | def randrange(self, start, stop=None, step=1, int=int, default=None, |
| 148 | maxwidth=1L<<BPF): |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 149 | """Choose a random item from range(start, stop[, step]). |
| 150 | |
| 151 | This fixes the problem with randint() which includes the |
| 152 | endpoint; in Python this is usually not what you want. |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 153 | Do not supply the 'int', 'default', and 'maxwidth' arguments. |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 154 | """ |
| 155 | |
| 156 | # This code is a bit messy to make it fast for the |
Tim Peters | 9146f27 | 2002-08-16 03:41:39 +0000 | [diff] [blame] | 157 | # common case while still doing adequate error checking. |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 158 | istart = int(start) |
| 159 | if istart != start: |
| 160 | raise ValueError, "non-integer arg 1 for randrange()" |
| 161 | if stop is default: |
| 162 | if istart > 0: |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 163 | if istart >= maxwidth: |
| 164 | return self._randbelow(istart) |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 165 | return int(self.random() * istart) |
| 166 | raise ValueError, "empty range for randrange()" |
Tim Peters | 9146f27 | 2002-08-16 03:41:39 +0000 | [diff] [blame] | 167 | |
| 168 | # stop argument supplied. |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 169 | istop = int(stop) |
| 170 | if istop != stop: |
| 171 | raise ValueError, "non-integer stop for randrange()" |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 172 | width = istop - istart |
| 173 | if step == 1 and width > 0: |
Tim Peters | 76ca1d4 | 2003-06-19 03:46:46 +0000 | [diff] [blame] | 174 | # Note that |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 175 | # int(istart + self.random()*width) |
Tim Peters | 76ca1d4 | 2003-06-19 03:46:46 +0000 | [diff] [blame] | 176 | # instead would be incorrect. For example, consider istart |
| 177 | # = -2 and istop = 0. Then the guts would be in |
| 178 | # -2.0 to 0.0 exclusive on both ends (ignoring that random() |
| 179 | # might return 0.0), and because int() truncates toward 0, the |
| 180 | # final result would be -1 or 0 (instead of -2 or -1). |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 181 | # istart + int(self.random()*width) |
Tim Peters | 76ca1d4 | 2003-06-19 03:46:46 +0000 | [diff] [blame] | 182 | # would also be incorrect, for a subtler reason: the RHS |
| 183 | # can return a long, and then randrange() would also return |
| 184 | # a long, but we're supposed to return an int (for backward |
| 185 | # compatibility). |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 186 | |
| 187 | if width >= maxwidth: |
Tim Peters | 58eb11c | 2004-01-18 20:29:55 +0000 | [diff] [blame] | 188 | return int(istart + self._randbelow(width)) |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 189 | return int(istart + int(self.random()*width)) |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 190 | if step == 1: |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 191 | raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width) |
Tim Peters | 9146f27 | 2002-08-16 03:41:39 +0000 | [diff] [blame] | 192 | |
| 193 | # Non-unit step argument supplied. |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 194 | istep = int(step) |
| 195 | if istep != step: |
| 196 | raise ValueError, "non-integer step for randrange()" |
| 197 | if istep > 0: |
Raymond Hettinger | ffdb8bb | 2004-09-27 15:29:05 +0000 | [diff] [blame] | 198 | n = (width + istep - 1) // istep |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 199 | elif istep < 0: |
Raymond Hettinger | ffdb8bb | 2004-09-27 15:29:05 +0000 | [diff] [blame] | 200 | n = (width + istep + 1) // istep |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 201 | else: |
| 202 | raise ValueError, "zero step for randrange()" |
| 203 | |
| 204 | if n <= 0: |
| 205 | raise ValueError, "empty range for randrange()" |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 206 | |
| 207 | if n >= maxwidth: |
Raymond Hettinger | 94547f7 | 2006-12-20 06:42:06 +0000 | [diff] [blame] | 208 | return istart + istep*self._randbelow(n) |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 209 | return istart + istep*int(self.random() * n) |
| 210 | |
| 211 | def randint(self, a, b): |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 212 | """Return random integer in range [a, b], including both end points. |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 213 | """ |
| 214 | |
| 215 | return self.randrange(a, b+1) |
| 216 | |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 217 | def _randbelow(self, n, _log=_log, int=int, _maxwidth=1L<<BPF, |
| 218 | _Method=_MethodType, _BuiltinMethod=_BuiltinMethodType): |
| 219 | """Return a random int in the range [0,n) |
| 220 | |
| 221 | Handles the case where n has more bits than returned |
| 222 | by a single call to the underlying generator. |
| 223 | """ |
| 224 | |
| 225 | try: |
| 226 | getrandbits = self.getrandbits |
| 227 | except AttributeError: |
| 228 | pass |
| 229 | else: |
| 230 | # Only call self.getrandbits if the original random() builtin method |
| 231 | # has not been overridden or if a new getrandbits() was supplied. |
| 232 | # This assures that the two methods correspond. |
| 233 | if type(self.random) is _BuiltinMethod or type(getrandbits) is _Method: |
| 234 | k = int(1.00001 + _log(n-1, 2.0)) # 2**k > n-1 > 2**(k-2) |
| 235 | r = getrandbits(k) |
| 236 | while r >= n: |
| 237 | r = getrandbits(k) |
| 238 | return r |
| 239 | if n >= _maxwidth: |
| 240 | _warn("Underlying random() generator does not supply \n" |
| 241 | "enough bits to choose from a population range this large") |
| 242 | return int(self.random() * n) |
| 243 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 244 | ## -------------------- sequence methods ------------------- |
| 245 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 246 | def choice(self, seq): |
| 247 | """Choose a random element from a non-empty sequence.""" |
Raymond Hettinger | 5dae505 | 2004-06-07 02:07:15 +0000 | [diff] [blame] | 248 | return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 249 | |
| 250 | def shuffle(self, x, random=None, int=int): |
| 251 | """x, random=random.random -> shuffle list x in place; return None. |
| 252 | |
| 253 | Optional arg random is a 0-argument function returning a random |
| 254 | float in [0.0, 1.0); by default, the standard random.random. |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 255 | """ |
| 256 | |
| 257 | if random is None: |
| 258 | random = self.random |
Raymond Hettinger | 85c20a4 | 2003-11-06 14:06:48 +0000 | [diff] [blame] | 259 | for i in reversed(xrange(1, len(x))): |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 260 | # pick an element in x[:i+1] with which to exchange x[i] |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 261 | j = int(random() * (i+1)) |
| 262 | x[i], x[j] = x[j], x[i] |
| 263 | |
Raymond Hettinger | fdbe522 | 2003-06-13 07:01:51 +0000 | [diff] [blame] | 264 | def sample(self, population, k): |
Raymond Hettinger | f24eb35 | 2002-11-12 17:41:57 +0000 | [diff] [blame] | 265 | """Chooses k unique random elements from a population sequence. |
| 266 | |
Raymond Hettinger | c0b4034 | 2002-11-13 15:26:37 +0000 | [diff] [blame] | 267 | Returns a new list containing elements from the population while |
| 268 | leaving the original population unchanged. The resulting list is |
| 269 | in selection order so that all sub-slices will also be valid random |
| 270 | samples. This allows raffle winners (the sample) to be partitioned |
| 271 | into grand prize and second place winners (the subslices). |
Raymond Hettinger | f24eb35 | 2002-11-12 17:41:57 +0000 | [diff] [blame] | 272 | |
Raymond Hettinger | c0b4034 | 2002-11-13 15:26:37 +0000 | [diff] [blame] | 273 | Members of the population need not be hashable or unique. If the |
| 274 | population contains repeats, then each occurrence is a possible |
| 275 | selection in the sample. |
Raymond Hettinger | f24eb35 | 2002-11-12 17:41:57 +0000 | [diff] [blame] | 276 | |
Raymond Hettinger | c0b4034 | 2002-11-13 15:26:37 +0000 | [diff] [blame] | 277 | To choose a sample in a range of integers, use xrange as an argument. |
| 278 | This is especially fast and space efficient for sampling from a |
| 279 | large population: sample(xrange(10000000), 60) |
Raymond Hettinger | f24eb35 | 2002-11-12 17:41:57 +0000 | [diff] [blame] | 280 | """ |
| 281 | |
Tim Peters | c17976e | 2006-04-01 00:26:53 +0000 | [diff] [blame] | 282 | # XXX Although the documentation says `population` is "a sequence", |
| 283 | # XXX attempts are made to cater to any iterable with a __len__ |
| 284 | # XXX method. This has had mixed success. Examples from both |
| 285 | # XXX sides: sets work fine, and should become officially supported; |
| 286 | # XXX dicts are much harder, and have failed in various subtle |
| 287 | # XXX ways across attempts. Support for mapping types should probably |
| 288 | # XXX be dropped (and users should pass mapping.keys() or .values() |
| 289 | # XXX explicitly). |
| 290 | |
Raymond Hettinger | c0b4034 | 2002-11-13 15:26:37 +0000 | [diff] [blame] | 291 | # Sampling without replacement entails tracking either potential |
Raymond Hettinger | 91e27c2 | 2005-08-19 01:36:35 +0000 | [diff] [blame] | 292 | # selections (the pool) in a list or previous selections in a set. |
Raymond Hettinger | c0b4034 | 2002-11-13 15:26:37 +0000 | [diff] [blame] | 293 | |
Jeremy Hylton | 2b55d35 | 2004-02-23 17:27:57 +0000 | [diff] [blame] | 294 | # When the number of selections is small compared to the |
| 295 | # population, then tracking selections is efficient, requiring |
Raymond Hettinger | 91e27c2 | 2005-08-19 01:36:35 +0000 | [diff] [blame] | 296 | # only a small set and an occasional reselection. For |
Jeremy Hylton | 2b55d35 | 2004-02-23 17:27:57 +0000 | [diff] [blame] | 297 | # a larger number of selections, the pool tracking method is |
| 298 | # preferred since the list takes less space than the |
Raymond Hettinger | 91e27c2 | 2005-08-19 01:36:35 +0000 | [diff] [blame] | 299 | # set and it doesn't suffer from frequent reselections. |
Raymond Hettinger | c0b4034 | 2002-11-13 15:26:37 +0000 | [diff] [blame] | 300 | |
Raymond Hettinger | f24eb35 | 2002-11-12 17:41:57 +0000 | [diff] [blame] | 301 | n = len(population) |
| 302 | if not 0 <= k <= n: |
| 303 | raise ValueError, "sample larger than population" |
Raymond Hettinger | 8b9aa8d | 2003-01-04 05:20:33 +0000 | [diff] [blame] | 304 | random = self.random |
Raymond Hettinger | fdbe522 | 2003-06-13 07:01:51 +0000 | [diff] [blame] | 305 | _int = int |
Raymond Hettinger | c0b4034 | 2002-11-13 15:26:37 +0000 | [diff] [blame] | 306 | result = [None] * k |
Raymond Hettinger | 91e27c2 | 2005-08-19 01:36:35 +0000 | [diff] [blame] | 307 | setsize = 21 # size of a small set minus size of an empty list |
| 308 | if k > 5: |
Tim Peters | 9e34c04 | 2005-08-26 15:20:46 +0000 | [diff] [blame] | 309 | setsize += 4 ** _ceil(_log(k * 3, 4)) # table size for big sets |
Tim Peters | c17976e | 2006-04-01 00:26:53 +0000 | [diff] [blame] | 310 | if n <= setsize or hasattr(population, "keys"): |
| 311 | # An n-length list is smaller than a k-length set, or this is a |
| 312 | # mapping type so the other algorithm wouldn't work. |
Raymond Hettinger | 311f419 | 2002-11-18 09:01:24 +0000 | [diff] [blame] | 313 | pool = list(population) |
| 314 | for i in xrange(k): # invariant: non-selected at [0,n-i) |
Raymond Hettinger | fdbe522 | 2003-06-13 07:01:51 +0000 | [diff] [blame] | 315 | j = _int(random() * (n-i)) |
Raymond Hettinger | 311f419 | 2002-11-18 09:01:24 +0000 | [diff] [blame] | 316 | result[i] = pool[j] |
Raymond Hettinger | 8b9aa8d | 2003-01-04 05:20:33 +0000 | [diff] [blame] | 317 | pool[j] = pool[n-i-1] # move non-selected item into vacancy |
Raymond Hettinger | c0b4034 | 2002-11-13 15:26:37 +0000 | [diff] [blame] | 318 | else: |
Raymond Hettinger | 66d09f1 | 2003-09-06 04:25:54 +0000 | [diff] [blame] | 319 | try: |
Raymond Hettinger | 3c3346d | 2006-03-29 09:13:13 +0000 | [diff] [blame] | 320 | selected = set() |
| 321 | selected_add = selected.add |
| 322 | for i in xrange(k): |
Raymond Hettinger | fdbe522 | 2003-06-13 07:01:51 +0000 | [diff] [blame] | 323 | j = _int(random() * n) |
Raymond Hettinger | 3c3346d | 2006-03-29 09:13:13 +0000 | [diff] [blame] | 324 | while j in selected: |
| 325 | j = _int(random() * n) |
| 326 | selected_add(j) |
| 327 | result[i] = population[j] |
Tim Peters | c17976e | 2006-04-01 00:26:53 +0000 | [diff] [blame] | 328 | except (TypeError, KeyError): # handle (at least) sets |
Raymond Hettinger | 3c3346d | 2006-03-29 09:13:13 +0000 | [diff] [blame] | 329 | if isinstance(population, list): |
| 330 | raise |
Tim Peters | c17976e | 2006-04-01 00:26:53 +0000 | [diff] [blame] | 331 | return self.sample(tuple(population), k) |
Raymond Hettinger | 311f419 | 2002-11-18 09:01:24 +0000 | [diff] [blame] | 332 | return result |
Raymond Hettinger | f24eb35 | 2002-11-12 17:41:57 +0000 | [diff] [blame] | 333 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 334 | ## -------------------- real-valued distributions ------------------- |
| 335 | |
| 336 | ## -------------------- uniform distribution ------------------- |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 337 | |
| 338 | def uniform(self, a, b): |
| 339 | """Get a random number in the range [a, b).""" |
| 340 | return a + (b-a) * self.random() |
Guido van Rossum | ff03b1a | 1994-03-09 12:55:02 +0000 | [diff] [blame] | 341 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 342 | ## -------------------- normal distribution -------------------- |
Guido van Rossum | ff03b1a | 1994-03-09 12:55:02 +0000 | [diff] [blame] | 343 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 344 | def normalvariate(self, mu, sigma): |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 345 | """Normal distribution. |
| 346 | |
| 347 | mu is the mean, and sigma is the standard deviation. |
Raymond Hettinger | ef4d4bd | 2002-05-23 23:58:17 +0000 | [diff] [blame] | 348 | |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 349 | """ |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 350 | # mu = mean, sigma = standard deviation |
Guido van Rossum | ff03b1a | 1994-03-09 12:55:02 +0000 | [diff] [blame] | 351 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 352 | # Uses Kinderman and Monahan method. Reference: Kinderman, |
| 353 | # A.J. and Monahan, J.F., "Computer generation of random |
| 354 | # variables using the ratio of uniform deviates", ACM Trans |
| 355 | # Math Software, 3, (1977), pp257-260. |
Guido van Rossum | ff03b1a | 1994-03-09 12:55:02 +0000 | [diff] [blame] | 356 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 357 | random = self.random |
Raymond Hettinger | 42406e6 | 2005-04-30 09:02:51 +0000 | [diff] [blame] | 358 | while 1: |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 359 | u1 = random() |
Raymond Hettinger | 73ced7e | 2003-01-04 09:26:32 +0000 | [diff] [blame] | 360 | u2 = 1.0 - random() |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 361 | z = NV_MAGICCONST*(u1-0.5)/u2 |
| 362 | zz = z*z/4.0 |
| 363 | if zz <= -_log(u2): |
| 364 | break |
| 365 | return mu + z*sigma |
Guido van Rossum | ff03b1a | 1994-03-09 12:55:02 +0000 | [diff] [blame] | 366 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 367 | ## -------------------- lognormal distribution -------------------- |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 368 | |
| 369 | def lognormvariate(self, mu, sigma): |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 370 | """Log normal distribution. |
| 371 | |
| 372 | If you take the natural logarithm of this distribution, you'll get a |
| 373 | normal distribution with mean mu and standard deviation sigma. |
| 374 | mu can have any value, and sigma must be greater than zero. |
Raymond Hettinger | ef4d4bd | 2002-05-23 23:58:17 +0000 | [diff] [blame] | 375 | |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 376 | """ |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 377 | return _exp(self.normalvariate(mu, sigma)) |
| 378 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 379 | ## -------------------- exponential distribution -------------------- |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 380 | |
| 381 | def expovariate(self, lambd): |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 382 | """Exponential distribution. |
| 383 | |
| 384 | lambd is 1.0 divided by the desired mean. (The parameter would be |
| 385 | called "lambda", but that is a reserved word in Python.) Returned |
| 386 | values range from 0 to positive infinity. |
Raymond Hettinger | ef4d4bd | 2002-05-23 23:58:17 +0000 | [diff] [blame] | 387 | |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 388 | """ |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 389 | # lambd: rate lambd = 1/mean |
| 390 | # ('lambda' is a Python reserved word) |
| 391 | |
| 392 | random = self.random |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 393 | u = random() |
| 394 | while u <= 1e-7: |
| 395 | u = random() |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 396 | return -_log(u)/lambd |
Guido van Rossum | ff03b1a | 1994-03-09 12:55:02 +0000 | [diff] [blame] | 397 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 398 | ## -------------------- von Mises distribution -------------------- |
Guido van Rossum | ff03b1a | 1994-03-09 12:55:02 +0000 | [diff] [blame] | 399 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 400 | def vonmisesvariate(self, mu, kappa): |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 401 | """Circular data distribution. |
Raymond Hettinger | ef4d4bd | 2002-05-23 23:58:17 +0000 | [diff] [blame] | 402 | |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 403 | mu is the mean angle, expressed in radians between 0 and 2*pi, and |
| 404 | kappa is the concentration parameter, which must be greater than or |
| 405 | equal to zero. If kappa is equal to zero, this distribution reduces |
| 406 | to a uniform random angle over the range 0 to 2*pi. |
Raymond Hettinger | ef4d4bd | 2002-05-23 23:58:17 +0000 | [diff] [blame] | 407 | |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 408 | """ |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 409 | # mu: mean angle (in radians between 0 and 2*pi) |
| 410 | # kappa: concentration parameter kappa (>= 0) |
| 411 | # if kappa = 0 generate uniform random angle |
| 412 | |
| 413 | # Based upon an algorithm published in: Fisher, N.I., |
| 414 | # "Statistical Analysis of Circular Data", Cambridge |
| 415 | # University Press, 1993. |
| 416 | |
| 417 | # Thanks to Magnus Kessler for a correction to the |
| 418 | # implementation of step 4. |
| 419 | |
| 420 | random = self.random |
| 421 | if kappa <= 1e-6: |
| 422 | return TWOPI * random() |
| 423 | |
| 424 | a = 1.0 + _sqrt(1.0 + 4.0 * kappa * kappa) |
| 425 | b = (a - _sqrt(2.0 * a))/(2.0 * kappa) |
| 426 | r = (1.0 + b * b)/(2.0 * b) |
Guido van Rossum | ff03b1a | 1994-03-09 12:55:02 +0000 | [diff] [blame] | 427 | |
Raymond Hettinger | 42406e6 | 2005-04-30 09:02:51 +0000 | [diff] [blame] | 428 | while 1: |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 429 | u1 = random() |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 430 | |
| 431 | z = _cos(_pi * u1) |
| 432 | f = (1.0 + r * z)/(r + z) |
| 433 | c = kappa * (r - f) |
| 434 | |
| 435 | u2 = random() |
| 436 | |
Raymond Hettinger | 42406e6 | 2005-04-30 09:02:51 +0000 | [diff] [blame] | 437 | if u2 < c * (2.0 - c) or u2 <= c * _exp(1.0 - c): |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 438 | break |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 439 | |
| 440 | u3 = random() |
| 441 | if u3 > 0.5: |
| 442 | theta = (mu % TWOPI) + _acos(f) |
| 443 | else: |
| 444 | theta = (mu % TWOPI) - _acos(f) |
| 445 | |
| 446 | return theta |
| 447 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 448 | ## -------------------- gamma distribution -------------------- |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 449 | |
| 450 | def gammavariate(self, alpha, beta): |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 451 | """Gamma distribution. Not the gamma function! |
| 452 | |
| 453 | Conditions on the parameters are alpha > 0 and beta > 0. |
| 454 | |
| 455 | """ |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 456 | |
Raymond Hettinger | b760efb | 2002-05-14 06:40:34 +0000 | [diff] [blame] | 457 | # alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2 |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 458 | |
Guido van Rossum | 570764d | 2002-05-14 14:08:12 +0000 | [diff] [blame] | 459 | # Warning: a few older sources define the gamma distribution in terms |
| 460 | # of alpha > -1.0 |
| 461 | if alpha <= 0.0 or beta <= 0.0: |
| 462 | raise ValueError, 'gammavariate: alpha and beta must be > 0.0' |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 463 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 464 | random = self.random |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 465 | if alpha > 1.0: |
| 466 | |
| 467 | # Uses R.C.H. Cheng, "The generation of Gamma |
| 468 | # variables with non-integral shape parameters", |
| 469 | # Applied Statistics, (1977), 26, No. 1, p71-74 |
| 470 | |
Raymond Hettinger | ca6cdc2 | 2002-05-13 23:40:14 +0000 | [diff] [blame] | 471 | ainv = _sqrt(2.0 * alpha - 1.0) |
| 472 | bbb = alpha - LOG4 |
| 473 | ccc = alpha + ainv |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 474 | |
Raymond Hettinger | 42406e6 | 2005-04-30 09:02:51 +0000 | [diff] [blame] | 475 | while 1: |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 476 | u1 = random() |
Raymond Hettinger | 73ced7e | 2003-01-04 09:26:32 +0000 | [diff] [blame] | 477 | if not 1e-7 < u1 < .9999999: |
| 478 | continue |
| 479 | u2 = 1.0 - random() |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 480 | v = _log(u1/(1.0-u1))/ainv |
| 481 | x = alpha*_exp(v) |
| 482 | z = u1*u1*u2 |
| 483 | r = bbb+ccc*v-x |
| 484 | if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z): |
Raymond Hettinger | b760efb | 2002-05-14 06:40:34 +0000 | [diff] [blame] | 485 | return x * beta |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 486 | |
| 487 | elif alpha == 1.0: |
| 488 | # expovariate(1) |
| 489 | u = random() |
| 490 | while u <= 1e-7: |
| 491 | u = random() |
Raymond Hettinger | b760efb | 2002-05-14 06:40:34 +0000 | [diff] [blame] | 492 | return -_log(u) * beta |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 493 | |
| 494 | else: # alpha is between 0 and 1 (exclusive) |
| 495 | |
| 496 | # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle |
| 497 | |
Raymond Hettinger | 42406e6 | 2005-04-30 09:02:51 +0000 | [diff] [blame] | 498 | while 1: |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 499 | u = random() |
| 500 | b = (_e + alpha)/_e |
| 501 | p = b*u |
| 502 | if p <= 1.0: |
Raymond Hettinger | 42406e6 | 2005-04-30 09:02:51 +0000 | [diff] [blame] | 503 | x = p ** (1.0/alpha) |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 504 | else: |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 505 | x = -_log((b-p)/alpha) |
| 506 | u1 = random() |
Raymond Hettinger | 42406e6 | 2005-04-30 09:02:51 +0000 | [diff] [blame] | 507 | if p > 1.0: |
| 508 | if u1 <= x ** (alpha - 1.0): |
| 509 | break |
| 510 | elif u1 <= _exp(-x): |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 511 | break |
Raymond Hettinger | b760efb | 2002-05-14 06:40:34 +0000 | [diff] [blame] | 512 | return x * beta |
| 513 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 514 | ## -------------------- Gauss (faster alternative) -------------------- |
Guido van Rossum | 95bfcda | 1994-03-09 14:21:05 +0000 | [diff] [blame] | 515 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 516 | def gauss(self, mu, sigma): |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 517 | """Gaussian distribution. |
| 518 | |
| 519 | mu is the mean, and sigma is the standard deviation. This is |
| 520 | slightly faster than the normalvariate() function. |
| 521 | |
| 522 | Not thread-safe without a lock around calls. |
Raymond Hettinger | ef4d4bd | 2002-05-23 23:58:17 +0000 | [diff] [blame] | 523 | |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 524 | """ |
Guido van Rossum | cc32ac9 | 1994-03-15 16:10:24 +0000 | [diff] [blame] | 525 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 526 | # When x and y are two variables from [0, 1), uniformly |
| 527 | # distributed, then |
| 528 | # |
| 529 | # cos(2*pi*x)*sqrt(-2*log(1-y)) |
| 530 | # sin(2*pi*x)*sqrt(-2*log(1-y)) |
| 531 | # |
| 532 | # are two *independent* variables with normal distribution |
| 533 | # (mu = 0, sigma = 1). |
| 534 | # (Lambert Meertens) |
| 535 | # (corrected version; bug discovered by Mike Miller, fixed by LM) |
Guido van Rossum | cc32ac9 | 1994-03-15 16:10:24 +0000 | [diff] [blame] | 536 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 537 | # Multithreading note: When two threads call this function |
| 538 | # simultaneously, it is possible that they will receive the |
| 539 | # same return value. The window is very small though. To |
| 540 | # avoid this, you have to use a lock around all calls. (I |
| 541 | # didn't want to slow this down in the serial case by using a |
| 542 | # lock here.) |
Guido van Rossum | d03e119 | 1998-05-29 17:51:31 +0000 | [diff] [blame] | 543 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 544 | random = self.random |
| 545 | z = self.gauss_next |
| 546 | self.gauss_next = None |
| 547 | if z is None: |
| 548 | x2pi = random() * TWOPI |
| 549 | g2rad = _sqrt(-2.0 * _log(1.0 - random())) |
| 550 | z = _cos(x2pi) * g2rad |
| 551 | self.gauss_next = _sin(x2pi) * g2rad |
Guido van Rossum | cc32ac9 | 1994-03-15 16:10:24 +0000 | [diff] [blame] | 552 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 553 | return mu + z*sigma |
Guido van Rossum | 95bfcda | 1994-03-09 14:21:05 +0000 | [diff] [blame] | 554 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 555 | ## -------------------- beta -------------------- |
Tim Peters | 85e2e47 | 2001-01-26 06:49:56 +0000 | [diff] [blame] | 556 | ## See |
| 557 | ## http://sourceforge.net/bugs/?func=detailbug&bug_id=130030&group_id=5470 |
| 558 | ## for Ivan Frohne's insightful analysis of why the original implementation: |
| 559 | ## |
| 560 | ## def betavariate(self, alpha, beta): |
| 561 | ## # Discrete Event Simulation in C, pp 87-88. |
| 562 | ## |
| 563 | ## y = self.expovariate(alpha) |
| 564 | ## z = self.expovariate(1.0/beta) |
| 565 | ## return z/(y+z) |
| 566 | ## |
| 567 | ## was dead wrong, and how it probably got that way. |
Guido van Rossum | 95bfcda | 1994-03-09 14:21:05 +0000 | [diff] [blame] | 568 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 569 | def betavariate(self, alpha, beta): |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 570 | """Beta distribution. |
| 571 | |
Raymond Hettinger | 1b0ce85 | 2007-01-19 18:07:18 +0000 | [diff] [blame] | 572 | Conditions on the parameters are alpha > 0 and beta > 0. |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 573 | Returned values range between 0 and 1. |
Raymond Hettinger | ef4d4bd | 2002-05-23 23:58:17 +0000 | [diff] [blame] | 574 | |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 575 | """ |
Raymond Hettinger | ef4d4bd | 2002-05-23 23:58:17 +0000 | [diff] [blame] | 576 | |
Tim Peters | 85e2e47 | 2001-01-26 06:49:56 +0000 | [diff] [blame] | 577 | # This version due to Janne Sinkkonen, and matches all the std |
| 578 | # texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution"). |
| 579 | y = self.gammavariate(alpha, 1.) |
| 580 | if y == 0: |
| 581 | return 0.0 |
| 582 | else: |
| 583 | return y / (y + self.gammavariate(beta, 1.)) |
Guido van Rossum | 95bfcda | 1994-03-09 14:21:05 +0000 | [diff] [blame] | 584 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 585 | ## -------------------- Pareto -------------------- |
Guido van Rossum | cf4559a | 1997-12-02 02:47:39 +0000 | [diff] [blame] | 586 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 587 | def paretovariate(self, alpha): |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 588 | """Pareto distribution. alpha is the shape parameter.""" |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 589 | # Jain, pg. 495 |
Guido van Rossum | cf4559a | 1997-12-02 02:47:39 +0000 | [diff] [blame] | 590 | |
Raymond Hettinger | 73ced7e | 2003-01-04 09:26:32 +0000 | [diff] [blame] | 591 | u = 1.0 - self.random() |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 592 | return 1.0 / pow(u, 1.0/alpha) |
Guido van Rossum | cf4559a | 1997-12-02 02:47:39 +0000 | [diff] [blame] | 593 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 594 | ## -------------------- Weibull -------------------- |
Guido van Rossum | cf4559a | 1997-12-02 02:47:39 +0000 | [diff] [blame] | 595 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 596 | def weibullvariate(self, alpha, beta): |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 597 | """Weibull distribution. |
| 598 | |
| 599 | alpha is the scale parameter and beta is the shape parameter. |
Raymond Hettinger | ef4d4bd | 2002-05-23 23:58:17 +0000 | [diff] [blame] | 600 | |
Raymond Hettinger | c32f033 | 2002-05-23 19:44:49 +0000 | [diff] [blame] | 601 | """ |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 602 | # Jain, pg. 499; bug fix courtesy Bill Arms |
Guido van Rossum | cf4559a | 1997-12-02 02:47:39 +0000 | [diff] [blame] | 603 | |
Raymond Hettinger | 73ced7e | 2003-01-04 09:26:32 +0000 | [diff] [blame] | 604 | u = 1.0 - self.random() |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 605 | return alpha * pow(-_log(u), 1.0/beta) |
Guido van Rossum | 6c395ba | 1999-08-18 13:53:28 +0000 | [diff] [blame] | 606 | |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 607 | ## -------------------- Wichmann-Hill ------------------- |
| 608 | |
| 609 | class WichmannHill(Random): |
| 610 | |
| 611 | VERSION = 1 # used by getstate/setstate |
| 612 | |
| 613 | def seed(self, a=None): |
| 614 | """Initialize internal state from hashable object. |
| 615 | |
Raymond Hettinger | 23f1241 | 2004-09-13 22:23:21 +0000 | [diff] [blame] | 616 | None or no argument seeds from current time or from an operating |
| 617 | system specific randomness source if available. |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 618 | |
| 619 | If a is not None or an int or long, hash(a) is used instead. |
| 620 | |
| 621 | If a is an int or long, a is used directly. Distinct values between |
| 622 | 0 and 27814431486575L inclusive are guaranteed to yield distinct |
| 623 | internal states (this guarantee is specific to the default |
| 624 | Wichmann-Hill generator). |
| 625 | """ |
| 626 | |
| 627 | if a is None: |
Raymond Hettinger | c1c43ca | 2004-09-05 00:00:42 +0000 | [diff] [blame] | 628 | try: |
| 629 | a = long(_hexlify(_urandom(16)), 16) |
| 630 | except NotImplementedError: |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 631 | import time |
| 632 | a = long(time.time() * 256) # use fractional seconds |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 633 | |
| 634 | if not isinstance(a, (int, long)): |
| 635 | a = hash(a) |
| 636 | |
| 637 | a, x = divmod(a, 30268) |
| 638 | a, y = divmod(a, 30306) |
| 639 | a, z = divmod(a, 30322) |
| 640 | self._seed = int(x)+1, int(y)+1, int(z)+1 |
| 641 | |
| 642 | self.gauss_next = None |
| 643 | |
| 644 | def random(self): |
| 645 | """Get the next random number in the range [0.0, 1.0).""" |
| 646 | |
| 647 | # Wichman-Hill random number generator. |
| 648 | # |
| 649 | # Wichmann, B. A. & Hill, I. D. (1982) |
| 650 | # Algorithm AS 183: |
| 651 | # An efficient and portable pseudo-random number generator |
| 652 | # Applied Statistics 31 (1982) 188-190 |
| 653 | # |
| 654 | # see also: |
| 655 | # Correction to Algorithm AS 183 |
| 656 | # Applied Statistics 33 (1984) 123 |
| 657 | # |
| 658 | # McLeod, A. I. (1985) |
| 659 | # A remark on Algorithm AS 183 |
| 660 | # Applied Statistics 34 (1985),198-200 |
| 661 | |
| 662 | # This part is thread-unsafe: |
| 663 | # BEGIN CRITICAL SECTION |
| 664 | x, y, z = self._seed |
| 665 | x = (171 * x) % 30269 |
| 666 | y = (172 * y) % 30307 |
| 667 | z = (170 * z) % 30323 |
| 668 | self._seed = x, y, z |
| 669 | # END CRITICAL SECTION |
| 670 | |
| 671 | # Note: on a platform using IEEE-754 double arithmetic, this can |
| 672 | # never return 0.0 (asserted by Tim; proof too long for a comment). |
| 673 | return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0 |
| 674 | |
| 675 | def getstate(self): |
| 676 | """Return internal state; can be passed to setstate() later.""" |
| 677 | return self.VERSION, self._seed, self.gauss_next |
| 678 | |
| 679 | def setstate(self, state): |
| 680 | """Restore internal state from object returned by getstate().""" |
| 681 | version = state[0] |
| 682 | if version == 1: |
| 683 | version, self._seed, self.gauss_next = state |
| 684 | else: |
| 685 | raise ValueError("state with version %s passed to " |
| 686 | "Random.setstate() of version %s" % |
| 687 | (version, self.VERSION)) |
| 688 | |
| 689 | def jumpahead(self, n): |
| 690 | """Act as if n calls to random() were made, but quickly. |
| 691 | |
| 692 | n is an int, greater than or equal to 0. |
| 693 | |
| 694 | Example use: If you have 2 threads and know that each will |
| 695 | consume no more than a million random numbers, create two Random |
| 696 | objects r1 and r2, then do |
| 697 | r2.setstate(r1.getstate()) |
| 698 | r2.jumpahead(1000000) |
| 699 | Then r1 and r2 will use guaranteed-disjoint segments of the full |
| 700 | period. |
| 701 | """ |
| 702 | |
| 703 | if not n >= 0: |
| 704 | raise ValueError("n must be >= 0") |
| 705 | x, y, z = self._seed |
| 706 | x = int(x * pow(171, n, 30269)) % 30269 |
| 707 | y = int(y * pow(172, n, 30307)) % 30307 |
| 708 | z = int(z * pow(170, n, 30323)) % 30323 |
| 709 | self._seed = x, y, z |
| 710 | |
| 711 | def __whseed(self, x=0, y=0, z=0): |
| 712 | """Set the Wichmann-Hill seed from (x, y, z). |
| 713 | |
| 714 | These must be integers in the range [0, 256). |
| 715 | """ |
| 716 | |
| 717 | if not type(x) == type(y) == type(z) == int: |
| 718 | raise TypeError('seeds must be integers') |
| 719 | if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256): |
| 720 | raise ValueError('seeds must be in range(0, 256)') |
| 721 | if 0 == x == y == z: |
| 722 | # Initialize from current time |
| 723 | import time |
| 724 | t = long(time.time() * 256) |
| 725 | t = int((t&0xffffff) ^ (t>>24)) |
| 726 | t, x = divmod(t, 256) |
| 727 | t, y = divmod(t, 256) |
| 728 | t, z = divmod(t, 256) |
| 729 | # Zero is a poor seed, so substitute 1 |
| 730 | self._seed = (x or 1, y or 1, z or 1) |
| 731 | |
| 732 | self.gauss_next = None |
| 733 | |
| 734 | def whseed(self, a=None): |
| 735 | """Seed from hashable object's hash code. |
| 736 | |
| 737 | None or no argument seeds from current time. It is not guaranteed |
| 738 | that objects with distinct hash codes lead to distinct internal |
| 739 | states. |
| 740 | |
| 741 | This is obsolete, provided for compatibility with the seed routine |
| 742 | used prior to Python 2.1. Use the .seed() method instead. |
| 743 | """ |
| 744 | |
| 745 | if a is None: |
| 746 | self.__whseed() |
| 747 | return |
| 748 | a = hash(a) |
| 749 | a, x = divmod(a, 256) |
| 750 | a, y = divmod(a, 256) |
| 751 | a, z = divmod(a, 256) |
| 752 | x = (x + a) % 256 or 1 |
| 753 | y = (y + a) % 256 or 1 |
| 754 | z = (z + a) % 256 or 1 |
| 755 | self.__whseed(x, y, z) |
| 756 | |
Raymond Hettinger | 23f1241 | 2004-09-13 22:23:21 +0000 | [diff] [blame] | 757 | ## --------------- Operating System Random Source ------------------ |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 758 | |
Raymond Hettinger | 23f1241 | 2004-09-13 22:23:21 +0000 | [diff] [blame] | 759 | class SystemRandom(Random): |
| 760 | """Alternate random number generator using sources provided |
| 761 | by the operating system (such as /dev/urandom on Unix or |
| 762 | CryptGenRandom on Windows). |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 763 | |
| 764 | Not available on all systems (see os.urandom() for details). |
| 765 | """ |
| 766 | |
| 767 | def random(self): |
| 768 | """Get the next random number in the range [0.0, 1.0).""" |
Tim Peters | 7c2a85b | 2004-08-31 02:19:55 +0000 | [diff] [blame] | 769 | return (long(_hexlify(_urandom(7)), 16) >> 3) * RECIP_BPF |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 770 | |
| 771 | def getrandbits(self, k): |
| 772 | """getrandbits(k) -> x. Generates a long int with k random bits.""" |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 773 | if k <= 0: |
| 774 | raise ValueError('number of bits must be greater than zero') |
| 775 | if k != int(k): |
| 776 | raise TypeError('number of bits should be an integer') |
| 777 | bytes = (k + 7) // 8 # bits / 8 and rounded up |
| 778 | x = long(_hexlify(_urandom(bytes)), 16) |
| 779 | return x >> (bytes * 8 - k) # trim excess bits |
| 780 | |
| 781 | def _stub(self, *args, **kwds): |
Raymond Hettinger | 23f1241 | 2004-09-13 22:23:21 +0000 | [diff] [blame] | 782 | "Stub method. Not used for a system random number generator." |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 783 | return None |
| 784 | seed = jumpahead = _stub |
| 785 | |
| 786 | def _notimplemented(self, *args, **kwds): |
Raymond Hettinger | 23f1241 | 2004-09-13 22:23:21 +0000 | [diff] [blame] | 787 | "Method should not be called for a system random number generator." |
| 788 | raise NotImplementedError('System entropy source does not have state.') |
Raymond Hettinger | 356a459 | 2004-08-30 06:14:31 +0000 | [diff] [blame] | 789 | getstate = setstate = _notimplemented |
| 790 | |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 791 | ## -------------------- test program -------------------- |
Guido van Rossum | ff03b1a | 1994-03-09 12:55:02 +0000 | [diff] [blame] | 792 | |
Raymond Hettinger | 6229713 | 2003-08-30 01:24:19 +0000 | [diff] [blame] | 793 | def _test_generator(n, func, args): |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 794 | import time |
Raymond Hettinger | 6229713 | 2003-08-30 01:24:19 +0000 | [diff] [blame] | 795 | print n, 'times', func.__name__ |
Raymond Hettinger | b98154e | 2003-05-24 17:26:02 +0000 | [diff] [blame] | 796 | total = 0.0 |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 797 | sqsum = 0.0 |
| 798 | smallest = 1e10 |
| 799 | largest = -1e10 |
| 800 | t0 = time.time() |
| 801 | for i in range(n): |
Raymond Hettinger | 6229713 | 2003-08-30 01:24:19 +0000 | [diff] [blame] | 802 | x = func(*args) |
Raymond Hettinger | b98154e | 2003-05-24 17:26:02 +0000 | [diff] [blame] | 803 | total += x |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 804 | sqsum = sqsum + x*x |
| 805 | smallest = min(x, smallest) |
| 806 | largest = max(x, largest) |
| 807 | t1 = time.time() |
| 808 | print round(t1-t0, 3), 'sec,', |
Raymond Hettinger | b98154e | 2003-05-24 17:26:02 +0000 | [diff] [blame] | 809 | avg = total/n |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 810 | stddev = _sqrt(sqsum/n - avg*avg) |
Tim Peters | 0c9886d | 2001-01-15 01:18:21 +0000 | [diff] [blame] | 811 | print 'avg %g, stddev %g, min %g, max %g' % \ |
| 812 | (avg, stddev, smallest, largest) |
Guido van Rossum | ff03b1a | 1994-03-09 12:55:02 +0000 | [diff] [blame] | 813 | |
Raymond Hettinger | f24eb35 | 2002-11-12 17:41:57 +0000 | [diff] [blame] | 814 | |
| 815 | def _test(N=2000): |
Raymond Hettinger | 6229713 | 2003-08-30 01:24:19 +0000 | [diff] [blame] | 816 | _test_generator(N, random, ()) |
| 817 | _test_generator(N, normalvariate, (0.0, 1.0)) |
| 818 | _test_generator(N, lognormvariate, (0.0, 1.0)) |
| 819 | _test_generator(N, vonmisesvariate, (0.0, 1.0)) |
| 820 | _test_generator(N, gammavariate, (0.01, 1.0)) |
| 821 | _test_generator(N, gammavariate, (0.1, 1.0)) |
| 822 | _test_generator(N, gammavariate, (0.1, 2.0)) |
| 823 | _test_generator(N, gammavariate, (0.5, 1.0)) |
| 824 | _test_generator(N, gammavariate, (0.9, 1.0)) |
| 825 | _test_generator(N, gammavariate, (1.0, 1.0)) |
| 826 | _test_generator(N, gammavariate, (2.0, 1.0)) |
| 827 | _test_generator(N, gammavariate, (20.0, 1.0)) |
| 828 | _test_generator(N, gammavariate, (200.0, 1.0)) |
| 829 | _test_generator(N, gauss, (0.0, 1.0)) |
| 830 | _test_generator(N, betavariate, (3.0, 3.0)) |
Tim Peters | cd80410 | 2001-01-25 20:25:57 +0000 | [diff] [blame] | 831 | |
Tim Peters | 715c4c4 | 2001-01-26 22:56:56 +0000 | [diff] [blame] | 832 | # Create one instance, seeded from current time, and export its methods |
Raymond Hettinger | 40f6217 | 2002-12-29 23:03:38 +0000 | [diff] [blame] | 833 | # as module-level functions. The functions share state across all uses |
| 834 | #(both in the user's code and in the Python libraries), but that's fine |
| 835 | # for most programs and is easier for the casual user than making them |
| 836 | # instantiate their own Random() instance. |
| 837 | |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 838 | _inst = Random() |
| 839 | seed = _inst.seed |
| 840 | random = _inst.random |
| 841 | uniform = _inst.uniform |
| 842 | randint = _inst.randint |
| 843 | choice = _inst.choice |
| 844 | randrange = _inst.randrange |
Raymond Hettinger | f24eb35 | 2002-11-12 17:41:57 +0000 | [diff] [blame] | 845 | sample = _inst.sample |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 846 | shuffle = _inst.shuffle |
| 847 | normalvariate = _inst.normalvariate |
| 848 | lognormvariate = _inst.lognormvariate |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 849 | expovariate = _inst.expovariate |
| 850 | vonmisesvariate = _inst.vonmisesvariate |
| 851 | gammavariate = _inst.gammavariate |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 852 | gauss = _inst.gauss |
| 853 | betavariate = _inst.betavariate |
| 854 | paretovariate = _inst.paretovariate |
| 855 | weibullvariate = _inst.weibullvariate |
| 856 | getstate = _inst.getstate |
| 857 | setstate = _inst.setstate |
Tim Peters | d52269b | 2001-01-25 06:23:18 +0000 | [diff] [blame] | 858 | jumpahead = _inst.jumpahead |
Raymond Hettinger | 2f726e9 | 2003-10-05 09:09:15 +0000 | [diff] [blame] | 859 | getrandbits = _inst.getrandbits |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 860 | |
Guido van Rossum | ff03b1a | 1994-03-09 12:55:02 +0000 | [diff] [blame] | 861 | if __name__ == '__main__': |
Tim Peters | d7b5e88 | 2001-01-25 03:36:26 +0000 | [diff] [blame] | 862 | _test() |