blob: 591686419cb63bee1972985e24a7f783f7f9ae8a [file] [log] [blame]
Guido van Rossume7b146f2000-02-04 15:28:42 +00001"""Random variable generators.
Guido van Rossumff03b1a1994-03-09 12:55:02 +00002
Tim Petersd7b5e882001-01-25 03:36:26 +00003 integers
4 --------
5 uniform within range
6
7 sequences
8 ---------
9 pick random element
Raymond Hettingerf24eb352002-11-12 17:41:57 +000010 pick random sample
Tim Petersd7b5e882001-01-25 03:36:26 +000011 generate random permutation
12
Guido van Rossume7b146f2000-02-04 15:28:42 +000013 distributions on the real line:
14 ------------------------------
Tim Petersd7b5e882001-01-25 03:36:26 +000015 uniform
Guido van Rossume7b146f2000-02-04 15:28:42 +000016 normal (Gaussian)
17 lognormal
18 negative exponential
19 gamma
20 beta
Raymond Hettinger40f62172002-12-29 23:03:38 +000021 pareto
22 Weibull
Guido van Rossumff03b1a1994-03-09 12:55:02 +000023
Guido van Rossume7b146f2000-02-04 15:28:42 +000024 distributions on the circle (angles 0 to 2pi)
25 ---------------------------------------------
26 circular uniform
27 von Mises
28
Raymond Hettinger40f62172002-12-29 23:03:38 +000029General notes on the underlying Mersenne Twister core generator:
Guido van Rossume7b146f2000-02-04 15:28:42 +000030
Raymond Hettinger40f62172002-12-29 23:03:38 +000031* The period is 2**19937-1.
32* It is one of the most extensively tested generators in existence
33* Without a direct way to compute N steps forward, the
34 semantics of jumpahead(n) are weakened to simply jump
35 to another distant state and rely on the large period
36 to avoid overlapping sequences.
37* The random() method is implemented in C, executes in
38 a single Python step, and is, therefore, threadsafe.
Tim Peterse360d952001-01-26 10:00:39 +000039
Guido van Rossume7b146f2000-02-04 15:28:42 +000040"""
Guido van Rossumd03e1191998-05-29 17:51:31 +000041
Raymond Hettinger2f726e92003-10-05 09:09:15 +000042from warnings import warn as _warn
43from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType
Tim Petersd7b5e882001-01-25 03:36:26 +000044from math import log as _log, exp as _exp, pi as _pi, e as _e
45from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
Tim Peters9146f272002-08-16 03:41:39 +000046from math import floor as _floor
Guido van Rossumff03b1a1994-03-09 12:55:02 +000047
Raymond Hettingerf24eb352002-11-12 17:41:57 +000048__all__ = ["Random","seed","random","uniform","randint","choice","sample",
Skip Montanaro0de65802001-02-15 22:15:14 +000049 "randrange","shuffle","normalvariate","lognormvariate",
Raymond Hettingerf8a52d32003-08-05 12:23:19 +000050 "expovariate","vonmisesvariate","gammavariate",
51 "gauss","betavariate","paretovariate","weibullvariate",
Raymond Hettinger2f726e92003-10-05 09:09:15 +000052 "getstate","setstate","jumpahead", "WichmannHill", "getrandbits",
53 "Random"]
Tim Petersd7b5e882001-01-25 03:36:26 +000054
55NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0)
Tim Petersd7b5e882001-01-25 03:36:26 +000056TWOPI = 2.0*_pi
Tim Petersd7b5e882001-01-25 03:36:26 +000057LOG4 = _log(4.0)
Tim Petersd7b5e882001-01-25 03:36:26 +000058SG_MAGICCONST = 1.0 + _log(4.5)
Raymond Hettinger2f726e92003-10-05 09:09:15 +000059BPF = 53 # Number of bits in a float
Tim Petersd7b5e882001-01-25 03:36:26 +000060
61# Translated by Guido van Rossum from C source provided by
Raymond Hettinger40f62172002-12-29 23:03:38 +000062# Adrian Baddeley. Adapted by Raymond Hettinger for use with
63# the Mersenne Twister core generator.
Tim Petersd7b5e882001-01-25 03:36:26 +000064
Raymond Hettinger145a4a02003-01-07 10:25:55 +000065import _random
Raymond Hettinger40f62172002-12-29 23:03:38 +000066
Raymond Hettinger145a4a02003-01-07 10:25:55 +000067class Random(_random.Random):
Raymond Hettingerc32f0332002-05-23 19:44:49 +000068 """Random number generator base class used by bound module functions.
69
70 Used to instantiate instances of Random to get generators that don't
71 share state. Especially useful for multi-threaded programs, creating
72 a different instance of Random for each thread, and using the jumpahead()
73 method to ensure that the generated sequences seen by each thread don't
74 overlap.
75
76 Class Random can also be subclassed if you want to use a different basic
77 generator of your own devising: in that case, override the following
78 methods: random(), seed(), getstate(), setstate() and jumpahead().
Raymond Hettinger2f726e92003-10-05 09:09:15 +000079 Optionally, implement a getrandombits() method so that randrange()
80 can cover arbitrarily large ranges.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +000081
Raymond Hettingerc32f0332002-05-23 19:44:49 +000082 """
Tim Petersd7b5e882001-01-25 03:36:26 +000083
Raymond Hettinger40f62172002-12-29 23:03:38 +000084 VERSION = 2 # used by getstate/setstate
Tim Petersd7b5e882001-01-25 03:36:26 +000085
86 def __init__(self, x=None):
87 """Initialize an instance.
88
89 Optional argument x controls seeding, as for Random.seed().
90 """
91
92 self.seed(x)
Raymond Hettinger40f62172002-12-29 23:03:38 +000093 self.gauss_next = None
Tim Petersd7b5e882001-01-25 03:36:26 +000094
Tim Peters0de88fc2001-02-01 04:59:18 +000095 def seed(self, a=None):
96 """Initialize internal state from hashable object.
Tim Petersd7b5e882001-01-25 03:36:26 +000097
Tim Peters0de88fc2001-02-01 04:59:18 +000098 None or no argument seeds from current time.
99
Tim Petersbcd725f2001-02-01 10:06:53 +0000100 If a is not None or an int or long, hash(a) is used instead.
Tim Petersd7b5e882001-01-25 03:36:26 +0000101 """
102
Raymond Hettinger3081d592003-08-09 18:30:57 +0000103 if a is None:
104 import time
105 a = long(time.time() * 256) # use fractional seconds
Raymond Hettinger145a4a02003-01-07 10:25:55 +0000106 super(Random, self).seed(a)
Tim Peters46c04e12002-05-05 20:40:00 +0000107 self.gauss_next = None
108
Tim Peterscd804102001-01-25 20:25:57 +0000109 def getstate(self):
110 """Return internal state; can be passed to setstate() later."""
Raymond Hettinger145a4a02003-01-07 10:25:55 +0000111 return self.VERSION, super(Random, self).getstate(), self.gauss_next
Tim Peterscd804102001-01-25 20:25:57 +0000112
113 def setstate(self, state):
114 """Restore internal state from object returned by getstate()."""
115 version = state[0]
Raymond Hettinger40f62172002-12-29 23:03:38 +0000116 if version == 2:
117 version, internalstate, self.gauss_next = state
Raymond Hettinger145a4a02003-01-07 10:25:55 +0000118 super(Random, self).setstate(internalstate)
Tim Peterscd804102001-01-25 20:25:57 +0000119 else:
120 raise ValueError("state with version %s passed to "
121 "Random.setstate() of version %s" %
122 (version, self.VERSION))
123
Tim Peterscd804102001-01-25 20:25:57 +0000124## ---- Methods below this point do not need to be overridden when
125## ---- subclassing for the purpose of using a different core generator.
126
127## -------------------- pickle support -------------------
128
129 def __getstate__(self): # for pickle
130 return self.getstate()
131
132 def __setstate__(self, state): # for pickle
133 self.setstate(state)
134
Raymond Hettinger5f078ff2003-06-24 20:29:04 +0000135 def __reduce__(self):
136 return self.__class__, (), self.getstate()
137
Tim Peterscd804102001-01-25 20:25:57 +0000138## -------------------- integer methods -------------------
139
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000140 def randrange(self, start, stop=None, step=1, int=int, default=None,
141 maxwidth=1L<<BPF):
Tim Petersd7b5e882001-01-25 03:36:26 +0000142 """Choose a random item from range(start, stop[, step]).
143
144 This fixes the problem with randint() which includes the
145 endpoint; in Python this is usually not what you want.
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000146 Do not supply the 'int', 'default', and 'maxwidth' arguments.
Tim Petersd7b5e882001-01-25 03:36:26 +0000147 """
148
149 # This code is a bit messy to make it fast for the
Tim Peters9146f272002-08-16 03:41:39 +0000150 # common case while still doing adequate error checking.
Tim Petersd7b5e882001-01-25 03:36:26 +0000151 istart = int(start)
152 if istart != start:
153 raise ValueError, "non-integer arg 1 for randrange()"
154 if stop is default:
155 if istart > 0:
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000156 if istart >= maxwidth:
157 return self._randbelow(istart)
Tim Petersd7b5e882001-01-25 03:36:26 +0000158 return int(self.random() * istart)
159 raise ValueError, "empty range for randrange()"
Tim Peters9146f272002-08-16 03:41:39 +0000160
161 # stop argument supplied.
Tim Petersd7b5e882001-01-25 03:36:26 +0000162 istop = int(stop)
163 if istop != stop:
164 raise ValueError, "non-integer stop for randrange()"
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000165 width = istop - istart
166 if step == 1 and width > 0:
Tim Peters76ca1d42003-06-19 03:46:46 +0000167 # Note that
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000168 # int(istart + self.random()*width)
Tim Peters76ca1d42003-06-19 03:46:46 +0000169 # instead would be incorrect. For example, consider istart
170 # = -2 and istop = 0. Then the guts would be in
171 # -2.0 to 0.0 exclusive on both ends (ignoring that random()
172 # might return 0.0), and because int() truncates toward 0, the
173 # final result would be -1 or 0 (instead of -2 or -1).
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000174 # istart + int(self.random()*width)
Tim Peters76ca1d42003-06-19 03:46:46 +0000175 # would also be incorrect, for a subtler reason: the RHS
176 # can return a long, and then randrange() would also return
177 # a long, but we're supposed to return an int (for backward
178 # compatibility).
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000179
180 if width >= maxwidth:
181 return int(istart + self._randbelow(width))
182 return int(istart + int(self.random()*width))
Tim Petersd7b5e882001-01-25 03:36:26 +0000183 if step == 1:
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000184 raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width)
Tim Peters9146f272002-08-16 03:41:39 +0000185
186 # Non-unit step argument supplied.
Tim Petersd7b5e882001-01-25 03:36:26 +0000187 istep = int(step)
188 if istep != step:
189 raise ValueError, "non-integer step for randrange()"
190 if istep > 0:
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000191 n = (width + istep - 1) / istep
Tim Petersd7b5e882001-01-25 03:36:26 +0000192 elif istep < 0:
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000193 n = (width + istep + 1) / istep
Tim Petersd7b5e882001-01-25 03:36:26 +0000194 else:
195 raise ValueError, "zero step for randrange()"
196
197 if n <= 0:
198 raise ValueError, "empty range for randrange()"
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000199
200 if n >= maxwidth:
201 return istart + self._randbelow(n)
Tim Petersd7b5e882001-01-25 03:36:26 +0000202 return istart + istep*int(self.random() * n)
203
204 def randint(self, a, b):
Tim Peterscd804102001-01-25 20:25:57 +0000205 """Return random integer in range [a, b], including both end points.
Tim Petersd7b5e882001-01-25 03:36:26 +0000206 """
207
208 return self.randrange(a, b+1)
209
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000210 def _randbelow(self, n, _log=_log, int=int, _maxwidth=1L<<BPF,
211 _Method=_MethodType, _BuiltinMethod=_BuiltinMethodType):
212 """Return a random int in the range [0,n)
213
214 Handles the case where n has more bits than returned
215 by a single call to the underlying generator.
216 """
217
218 try:
219 getrandbits = self.getrandbits
220 except AttributeError:
221 pass
222 else:
223 # Only call self.getrandbits if the original random() builtin method
224 # has not been overridden or if a new getrandbits() was supplied.
225 # This assures that the two methods correspond.
226 if type(self.random) is _BuiltinMethod or type(getrandbits) is _Method:
227 k = int(1.00001 + _log(n-1, 2.0)) # 2**k > n-1 > 2**(k-2)
228 r = getrandbits(k)
229 while r >= n:
230 r = getrandbits(k)
231 return r
232 if n >= _maxwidth:
233 _warn("Underlying random() generator does not supply \n"
234 "enough bits to choose from a population range this large")
235 return int(self.random() * n)
236
Tim Peterscd804102001-01-25 20:25:57 +0000237## -------------------- sequence methods -------------------
238
Tim Petersd7b5e882001-01-25 03:36:26 +0000239 def choice(self, seq):
240 """Choose a random element from a non-empty sequence."""
241 return seq[int(self.random() * len(seq))]
242
243 def shuffle(self, x, random=None, int=int):
244 """x, random=random.random -> shuffle list x in place; return None.
245
246 Optional arg random is a 0-argument function returning a random
247 float in [0.0, 1.0); by default, the standard random.random.
248
249 Note that for even rather small len(x), the total number of
250 permutations of x is larger than the period of most random number
251 generators; this implies that "most" permutations of a long
252 sequence can never be generated.
253 """
254
255 if random is None:
256 random = self.random
257 for i in xrange(len(x)-1, 0, -1):
Tim Peterscd804102001-01-25 20:25:57 +0000258 # pick an element in x[:i+1] with which to exchange x[i]
Tim Petersd7b5e882001-01-25 03:36:26 +0000259 j = int(random() * (i+1))
260 x[i], x[j] = x[j], x[i]
261
Raymond Hettingerfdbe5222003-06-13 07:01:51 +0000262 def sample(self, population, k):
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000263 """Chooses k unique random elements from a population sequence.
264
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000265 Returns a new list containing elements from the population while
266 leaving the original population unchanged. The resulting list is
267 in selection order so that all sub-slices will also be valid random
268 samples. This allows raffle winners (the sample) to be partitioned
269 into grand prize and second place winners (the subslices).
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000270
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000271 Members of the population need not be hashable or unique. If the
272 population contains repeats, then each occurrence is a possible
273 selection in the sample.
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000274
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000275 To choose a sample in a range of integers, use xrange as an argument.
276 This is especially fast and space efficient for sampling from a
277 large population: sample(xrange(10000000), 60)
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000278 """
279
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000280 # Sampling without replacement entails tracking either potential
Raymond Hettinger8b9aa8d2003-01-04 05:20:33 +0000281 # selections (the pool) in a list or previous selections in a
282 # dictionary.
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000283
Raymond Hettinger8b9aa8d2003-01-04 05:20:33 +0000284 # When the number of selections is small compared to the population,
285 # then tracking selections is efficient, requiring only a small
286 # dictionary and an occasional reselection. For a larger number of
287 # selections, the pool tracking method is preferred since the list takes
288 # less space than the dictionary and it doesn't suffer from frequent
289 # reselections.
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000290
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000291 n = len(population)
292 if not 0 <= k <= n:
293 raise ValueError, "sample larger than population"
Raymond Hettinger8b9aa8d2003-01-04 05:20:33 +0000294 random = self.random
Raymond Hettingerfdbe5222003-06-13 07:01:51 +0000295 _int = int
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000296 result = [None] * k
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000297 if n < 6 * k: # if n len list takes less space than a k len dict
Raymond Hettinger311f4192002-11-18 09:01:24 +0000298 pool = list(population)
299 for i in xrange(k): # invariant: non-selected at [0,n-i)
Raymond Hettingerfdbe5222003-06-13 07:01:51 +0000300 j = _int(random() * (n-i))
Raymond Hettinger311f4192002-11-18 09:01:24 +0000301 result[i] = pool[j]
Raymond Hettinger8b9aa8d2003-01-04 05:20:33 +0000302 pool[j] = pool[n-i-1] # move non-selected item into vacancy
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000303 else:
Raymond Hettinger66d09f12003-09-06 04:25:54 +0000304 try:
305 n > 0 and (population[0], population[n//2], population[n-1])
306 except (TypeError, KeyError): # handle sets and dictionaries
307 population = tuple(population)
Raymond Hettinger311f4192002-11-18 09:01:24 +0000308 selected = {}
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000309 for i in xrange(k):
Raymond Hettingerfdbe5222003-06-13 07:01:51 +0000310 j = _int(random() * n)
Raymond Hettinger311f4192002-11-18 09:01:24 +0000311 while j in selected:
Raymond Hettingerfdbe5222003-06-13 07:01:51 +0000312 j = _int(random() * n)
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000313 result[i] = selected[j] = population[j]
Raymond Hettinger311f4192002-11-18 09:01:24 +0000314 return result
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000315
Tim Peterscd804102001-01-25 20:25:57 +0000316## -------------------- real-valued distributions -------------------
317
318## -------------------- uniform distribution -------------------
Tim Petersd7b5e882001-01-25 03:36:26 +0000319
320 def uniform(self, a, b):
321 """Get a random number in the range [a, b)."""
322 return a + (b-a) * self.random()
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000323
Tim Peterscd804102001-01-25 20:25:57 +0000324## -------------------- normal distribution --------------------
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000325
Tim Petersd7b5e882001-01-25 03:36:26 +0000326 def normalvariate(self, mu, sigma):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000327 """Normal distribution.
328
329 mu is the mean, and sigma is the standard deviation.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000330
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000331 """
Tim Petersd7b5e882001-01-25 03:36:26 +0000332 # mu = mean, sigma = standard deviation
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000333
Tim Petersd7b5e882001-01-25 03:36:26 +0000334 # Uses Kinderman and Monahan method. Reference: Kinderman,
335 # A.J. and Monahan, J.F., "Computer generation of random
336 # variables using the ratio of uniform deviates", ACM Trans
337 # Math Software, 3, (1977), pp257-260.
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000338
Tim Petersd7b5e882001-01-25 03:36:26 +0000339 random = self.random
Raymond Hettinger311f4192002-11-18 09:01:24 +0000340 while True:
Tim Peters0c9886d2001-01-15 01:18:21 +0000341 u1 = random()
Raymond Hettinger73ced7e2003-01-04 09:26:32 +0000342 u2 = 1.0 - random()
Tim Petersd7b5e882001-01-25 03:36:26 +0000343 z = NV_MAGICCONST*(u1-0.5)/u2
344 zz = z*z/4.0
345 if zz <= -_log(u2):
346 break
347 return mu + z*sigma
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000348
Tim Peterscd804102001-01-25 20:25:57 +0000349## -------------------- lognormal distribution --------------------
Tim Petersd7b5e882001-01-25 03:36:26 +0000350
351 def lognormvariate(self, mu, sigma):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000352 """Log normal distribution.
353
354 If you take the natural logarithm of this distribution, you'll get a
355 normal distribution with mean mu and standard deviation sigma.
356 mu can have any value, and sigma must be greater than zero.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000357
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000358 """
Tim Petersd7b5e882001-01-25 03:36:26 +0000359 return _exp(self.normalvariate(mu, sigma))
360
Tim Peterscd804102001-01-25 20:25:57 +0000361## -------------------- exponential distribution --------------------
Tim Petersd7b5e882001-01-25 03:36:26 +0000362
363 def expovariate(self, lambd):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000364 """Exponential distribution.
365
366 lambd is 1.0 divided by the desired mean. (The parameter would be
367 called "lambda", but that is a reserved word in Python.) Returned
368 values range from 0 to positive infinity.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000369
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000370 """
Tim Petersd7b5e882001-01-25 03:36:26 +0000371 # lambd: rate lambd = 1/mean
372 # ('lambda' is a Python reserved word)
373
374 random = self.random
Tim Peters0c9886d2001-01-15 01:18:21 +0000375 u = random()
376 while u <= 1e-7:
377 u = random()
Tim Petersd7b5e882001-01-25 03:36:26 +0000378 return -_log(u)/lambd
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000379
Tim Peterscd804102001-01-25 20:25:57 +0000380## -------------------- von Mises distribution --------------------
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000381
Tim Petersd7b5e882001-01-25 03:36:26 +0000382 def vonmisesvariate(self, mu, kappa):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000383 """Circular data distribution.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000384
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000385 mu is the mean angle, expressed in radians between 0 and 2*pi, and
386 kappa is the concentration parameter, which must be greater than or
387 equal to zero. If kappa is equal to zero, this distribution reduces
388 to a uniform random angle over the range 0 to 2*pi.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000389
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000390 """
Tim Petersd7b5e882001-01-25 03:36:26 +0000391 # mu: mean angle (in radians between 0 and 2*pi)
392 # kappa: concentration parameter kappa (>= 0)
393 # if kappa = 0 generate uniform random angle
394
395 # Based upon an algorithm published in: Fisher, N.I.,
396 # "Statistical Analysis of Circular Data", Cambridge
397 # University Press, 1993.
398
399 # Thanks to Magnus Kessler for a correction to the
400 # implementation of step 4.
401
402 random = self.random
403 if kappa <= 1e-6:
404 return TWOPI * random()
405
406 a = 1.0 + _sqrt(1.0 + 4.0 * kappa * kappa)
407 b = (a - _sqrt(2.0 * a))/(2.0 * kappa)
408 r = (1.0 + b * b)/(2.0 * b)
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000409
Raymond Hettinger311f4192002-11-18 09:01:24 +0000410 while True:
Tim Peters0c9886d2001-01-15 01:18:21 +0000411 u1 = random()
Tim Petersd7b5e882001-01-25 03:36:26 +0000412
413 z = _cos(_pi * u1)
414 f = (1.0 + r * z)/(r + z)
415 c = kappa * (r - f)
416
417 u2 = random()
418
419 if not (u2 >= c * (2.0 - c) and u2 > c * _exp(1.0 - c)):
Tim Peters0c9886d2001-01-15 01:18:21 +0000420 break
Tim Petersd7b5e882001-01-25 03:36:26 +0000421
422 u3 = random()
423 if u3 > 0.5:
424 theta = (mu % TWOPI) + _acos(f)
425 else:
426 theta = (mu % TWOPI) - _acos(f)
427
428 return theta
429
Tim Peterscd804102001-01-25 20:25:57 +0000430## -------------------- gamma distribution --------------------
Tim Petersd7b5e882001-01-25 03:36:26 +0000431
432 def gammavariate(self, alpha, beta):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000433 """Gamma distribution. Not the gamma function!
434
435 Conditions on the parameters are alpha > 0 and beta > 0.
436
437 """
Tim Peters8ac14952002-05-23 15:15:30 +0000438
Raymond Hettingerb760efb2002-05-14 06:40:34 +0000439 # alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2
Tim Peters8ac14952002-05-23 15:15:30 +0000440
Guido van Rossum570764d2002-05-14 14:08:12 +0000441 # Warning: a few older sources define the gamma distribution in terms
442 # of alpha > -1.0
443 if alpha <= 0.0 or beta <= 0.0:
444 raise ValueError, 'gammavariate: alpha and beta must be > 0.0'
Tim Peters8ac14952002-05-23 15:15:30 +0000445
Tim Petersd7b5e882001-01-25 03:36:26 +0000446 random = self.random
Tim Petersd7b5e882001-01-25 03:36:26 +0000447 if alpha > 1.0:
448
449 # Uses R.C.H. Cheng, "The generation of Gamma
450 # variables with non-integral shape parameters",
451 # Applied Statistics, (1977), 26, No. 1, p71-74
452
Raymond Hettingerca6cdc22002-05-13 23:40:14 +0000453 ainv = _sqrt(2.0 * alpha - 1.0)
454 bbb = alpha - LOG4
455 ccc = alpha + ainv
Tim Peters8ac14952002-05-23 15:15:30 +0000456
Raymond Hettinger311f4192002-11-18 09:01:24 +0000457 while True:
Tim Petersd7b5e882001-01-25 03:36:26 +0000458 u1 = random()
Raymond Hettinger73ced7e2003-01-04 09:26:32 +0000459 if not 1e-7 < u1 < .9999999:
460 continue
461 u2 = 1.0 - random()
Tim Petersd7b5e882001-01-25 03:36:26 +0000462 v = _log(u1/(1.0-u1))/ainv
463 x = alpha*_exp(v)
464 z = u1*u1*u2
465 r = bbb+ccc*v-x
466 if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z):
Raymond Hettingerb760efb2002-05-14 06:40:34 +0000467 return x * beta
Tim Petersd7b5e882001-01-25 03:36:26 +0000468
469 elif alpha == 1.0:
470 # expovariate(1)
471 u = random()
472 while u <= 1e-7:
473 u = random()
Raymond Hettingerb760efb2002-05-14 06:40:34 +0000474 return -_log(u) * beta
Tim Petersd7b5e882001-01-25 03:36:26 +0000475
476 else: # alpha is between 0 and 1 (exclusive)
477
478 # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
479
Raymond Hettinger311f4192002-11-18 09:01:24 +0000480 while True:
Tim Petersd7b5e882001-01-25 03:36:26 +0000481 u = random()
482 b = (_e + alpha)/_e
483 p = b*u
484 if p <= 1.0:
485 x = pow(p, 1.0/alpha)
486 else:
487 # p > 1
488 x = -_log((b-p)/alpha)
489 u1 = random()
490 if not (((p <= 1.0) and (u1 > _exp(-x))) or
491 ((p > 1) and (u1 > pow(x, alpha - 1.0)))):
492 break
Raymond Hettingerb760efb2002-05-14 06:40:34 +0000493 return x * beta
494
Tim Peterscd804102001-01-25 20:25:57 +0000495## -------------------- Gauss (faster alternative) --------------------
Guido van Rossum95bfcda1994-03-09 14:21:05 +0000496
Tim Petersd7b5e882001-01-25 03:36:26 +0000497 def gauss(self, mu, sigma):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000498 """Gaussian distribution.
499
500 mu is the mean, and sigma is the standard deviation. This is
501 slightly faster than the normalvariate() function.
502
503 Not thread-safe without a lock around calls.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000504
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000505 """
Guido van Rossumcc32ac91994-03-15 16:10:24 +0000506
Tim Petersd7b5e882001-01-25 03:36:26 +0000507 # When x and y are two variables from [0, 1), uniformly
508 # distributed, then
509 #
510 # cos(2*pi*x)*sqrt(-2*log(1-y))
511 # sin(2*pi*x)*sqrt(-2*log(1-y))
512 #
513 # are two *independent* variables with normal distribution
514 # (mu = 0, sigma = 1).
515 # (Lambert Meertens)
516 # (corrected version; bug discovered by Mike Miller, fixed by LM)
Guido van Rossumcc32ac91994-03-15 16:10:24 +0000517
Tim Petersd7b5e882001-01-25 03:36:26 +0000518 # Multithreading note: When two threads call this function
519 # simultaneously, it is possible that they will receive the
520 # same return value. The window is very small though. To
521 # avoid this, you have to use a lock around all calls. (I
522 # didn't want to slow this down in the serial case by using a
523 # lock here.)
Guido van Rossumd03e1191998-05-29 17:51:31 +0000524
Tim Petersd7b5e882001-01-25 03:36:26 +0000525 random = self.random
526 z = self.gauss_next
527 self.gauss_next = None
528 if z is None:
529 x2pi = random() * TWOPI
530 g2rad = _sqrt(-2.0 * _log(1.0 - random()))
531 z = _cos(x2pi) * g2rad
532 self.gauss_next = _sin(x2pi) * g2rad
Guido van Rossumcc32ac91994-03-15 16:10:24 +0000533
Tim Petersd7b5e882001-01-25 03:36:26 +0000534 return mu + z*sigma
Guido van Rossum95bfcda1994-03-09 14:21:05 +0000535
Tim Peterscd804102001-01-25 20:25:57 +0000536## -------------------- beta --------------------
Tim Peters85e2e472001-01-26 06:49:56 +0000537## See
538## http://sourceforge.net/bugs/?func=detailbug&bug_id=130030&group_id=5470
539## for Ivan Frohne's insightful analysis of why the original implementation:
540##
541## def betavariate(self, alpha, beta):
542## # Discrete Event Simulation in C, pp 87-88.
543##
544## y = self.expovariate(alpha)
545## z = self.expovariate(1.0/beta)
546## return z/(y+z)
547##
548## was dead wrong, and how it probably got that way.
Guido van Rossum95bfcda1994-03-09 14:21:05 +0000549
Tim Petersd7b5e882001-01-25 03:36:26 +0000550 def betavariate(self, alpha, beta):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000551 """Beta distribution.
552
553 Conditions on the parameters are alpha > -1 and beta} > -1.
554 Returned values range between 0 and 1.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000555
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000556 """
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000557
Tim Peters85e2e472001-01-26 06:49:56 +0000558 # This version due to Janne Sinkkonen, and matches all the std
559 # texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution").
560 y = self.gammavariate(alpha, 1.)
561 if y == 0:
562 return 0.0
563 else:
564 return y / (y + self.gammavariate(beta, 1.))
Guido van Rossum95bfcda1994-03-09 14:21:05 +0000565
Tim Peterscd804102001-01-25 20:25:57 +0000566## -------------------- Pareto --------------------
Guido van Rossumcf4559a1997-12-02 02:47:39 +0000567
Tim Petersd7b5e882001-01-25 03:36:26 +0000568 def paretovariate(self, alpha):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000569 """Pareto distribution. alpha is the shape parameter."""
Tim Petersd7b5e882001-01-25 03:36:26 +0000570 # Jain, pg. 495
Guido van Rossumcf4559a1997-12-02 02:47:39 +0000571
Raymond Hettinger73ced7e2003-01-04 09:26:32 +0000572 u = 1.0 - self.random()
Tim Petersd7b5e882001-01-25 03:36:26 +0000573 return 1.0 / pow(u, 1.0/alpha)
Guido van Rossumcf4559a1997-12-02 02:47:39 +0000574
Tim Peterscd804102001-01-25 20:25:57 +0000575## -------------------- Weibull --------------------
Guido van Rossumcf4559a1997-12-02 02:47:39 +0000576
Tim Petersd7b5e882001-01-25 03:36:26 +0000577 def weibullvariate(self, alpha, beta):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000578 """Weibull distribution.
579
580 alpha is the scale parameter and beta is the shape parameter.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000581
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000582 """
Tim Petersd7b5e882001-01-25 03:36:26 +0000583 # Jain, pg. 499; bug fix courtesy Bill Arms
Guido van Rossumcf4559a1997-12-02 02:47:39 +0000584
Raymond Hettinger73ced7e2003-01-04 09:26:32 +0000585 u = 1.0 - self.random()
Tim Petersd7b5e882001-01-25 03:36:26 +0000586 return alpha * pow(-_log(u), 1.0/beta)
Guido van Rossum6c395ba1999-08-18 13:53:28 +0000587
Raymond Hettinger40f62172002-12-29 23:03:38 +0000588## -------------------- Wichmann-Hill -------------------
589
590class WichmannHill(Random):
591
592 VERSION = 1 # used by getstate/setstate
593
594 def seed(self, a=None):
595 """Initialize internal state from hashable object.
596
597 None or no argument seeds from current time.
598
599 If a is not None or an int or long, hash(a) is used instead.
600
601 If a is an int or long, a is used directly. Distinct values between
602 0 and 27814431486575L inclusive are guaranteed to yield distinct
603 internal states (this guarantee is specific to the default
604 Wichmann-Hill generator).
605 """
606
607 if a is None:
608 # Initialize from current time
609 import time
610 a = long(time.time() * 256)
611
612 if not isinstance(a, (int, long)):
613 a = hash(a)
614
615 a, x = divmod(a, 30268)
616 a, y = divmod(a, 30306)
617 a, z = divmod(a, 30322)
618 self._seed = int(x)+1, int(y)+1, int(z)+1
619
620 self.gauss_next = None
621
622 def random(self):
623 """Get the next random number in the range [0.0, 1.0)."""
624
625 # Wichman-Hill random number generator.
626 #
627 # Wichmann, B. A. & Hill, I. D. (1982)
628 # Algorithm AS 183:
629 # An efficient and portable pseudo-random number generator
630 # Applied Statistics 31 (1982) 188-190
631 #
632 # see also:
633 # Correction to Algorithm AS 183
634 # Applied Statistics 33 (1984) 123
635 #
636 # McLeod, A. I. (1985)
637 # A remark on Algorithm AS 183
638 # Applied Statistics 34 (1985),198-200
639
640 # This part is thread-unsafe:
641 # BEGIN CRITICAL SECTION
642 x, y, z = self._seed
643 x = (171 * x) % 30269
644 y = (172 * y) % 30307
645 z = (170 * z) % 30323
646 self._seed = x, y, z
647 # END CRITICAL SECTION
648
649 # Note: on a platform using IEEE-754 double arithmetic, this can
650 # never return 0.0 (asserted by Tim; proof too long for a comment).
651 return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
652
653 def getstate(self):
654 """Return internal state; can be passed to setstate() later."""
655 return self.VERSION, self._seed, self.gauss_next
656
657 def setstate(self, state):
658 """Restore internal state from object returned by getstate()."""
659 version = state[0]
660 if version == 1:
661 version, self._seed, self.gauss_next = state
662 else:
663 raise ValueError("state with version %s passed to "
664 "Random.setstate() of version %s" %
665 (version, self.VERSION))
666
667 def jumpahead(self, n):
668 """Act as if n calls to random() were made, but quickly.
669
670 n is an int, greater than or equal to 0.
671
672 Example use: If you have 2 threads and know that each will
673 consume no more than a million random numbers, create two Random
674 objects r1 and r2, then do
675 r2.setstate(r1.getstate())
676 r2.jumpahead(1000000)
677 Then r1 and r2 will use guaranteed-disjoint segments of the full
678 period.
679 """
680
681 if not n >= 0:
682 raise ValueError("n must be >= 0")
683 x, y, z = self._seed
684 x = int(x * pow(171, n, 30269)) % 30269
685 y = int(y * pow(172, n, 30307)) % 30307
686 z = int(z * pow(170, n, 30323)) % 30323
687 self._seed = x, y, z
688
689 def __whseed(self, x=0, y=0, z=0):
690 """Set the Wichmann-Hill seed from (x, y, z).
691
692 These must be integers in the range [0, 256).
693 """
694
695 if not type(x) == type(y) == type(z) == int:
696 raise TypeError('seeds must be integers')
697 if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
698 raise ValueError('seeds must be in range(0, 256)')
699 if 0 == x == y == z:
700 # Initialize from current time
701 import time
702 t = long(time.time() * 256)
703 t = int((t&0xffffff) ^ (t>>24))
704 t, x = divmod(t, 256)
705 t, y = divmod(t, 256)
706 t, z = divmod(t, 256)
707 # Zero is a poor seed, so substitute 1
708 self._seed = (x or 1, y or 1, z or 1)
709
710 self.gauss_next = None
711
712 def whseed(self, a=None):
713 """Seed from hashable object's hash code.
714
715 None or no argument seeds from current time. It is not guaranteed
716 that objects with distinct hash codes lead to distinct internal
717 states.
718
719 This is obsolete, provided for compatibility with the seed routine
720 used prior to Python 2.1. Use the .seed() method instead.
721 """
722
723 if a is None:
724 self.__whseed()
725 return
726 a = hash(a)
727 a, x = divmod(a, 256)
728 a, y = divmod(a, 256)
729 a, z = divmod(a, 256)
730 x = (x + a) % 256 or 1
731 y = (y + a) % 256 or 1
732 z = (z + a) % 256 or 1
733 self.__whseed(x, y, z)
734
Tim Peterscd804102001-01-25 20:25:57 +0000735## -------------------- test program --------------------
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000736
Raymond Hettinger62297132003-08-30 01:24:19 +0000737def _test_generator(n, func, args):
Tim Peters0c9886d2001-01-15 01:18:21 +0000738 import time
Raymond Hettinger62297132003-08-30 01:24:19 +0000739 print n, 'times', func.__name__
Raymond Hettingerb98154e2003-05-24 17:26:02 +0000740 total = 0.0
Tim Peters0c9886d2001-01-15 01:18:21 +0000741 sqsum = 0.0
742 smallest = 1e10
743 largest = -1e10
744 t0 = time.time()
745 for i in range(n):
Raymond Hettinger62297132003-08-30 01:24:19 +0000746 x = func(*args)
Raymond Hettingerb98154e2003-05-24 17:26:02 +0000747 total += x
Tim Peters0c9886d2001-01-15 01:18:21 +0000748 sqsum = sqsum + x*x
749 smallest = min(x, smallest)
750 largest = max(x, largest)
751 t1 = time.time()
752 print round(t1-t0, 3), 'sec,',
Raymond Hettingerb98154e2003-05-24 17:26:02 +0000753 avg = total/n
Tim Petersd7b5e882001-01-25 03:36:26 +0000754 stddev = _sqrt(sqsum/n - avg*avg)
Tim Peters0c9886d2001-01-15 01:18:21 +0000755 print 'avg %g, stddev %g, min %g, max %g' % \
756 (avg, stddev, smallest, largest)
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000757
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000758
759def _test(N=2000):
Raymond Hettinger62297132003-08-30 01:24:19 +0000760 _test_generator(N, random, ())
761 _test_generator(N, normalvariate, (0.0, 1.0))
762 _test_generator(N, lognormvariate, (0.0, 1.0))
763 _test_generator(N, vonmisesvariate, (0.0, 1.0))
764 _test_generator(N, gammavariate, (0.01, 1.0))
765 _test_generator(N, gammavariate, (0.1, 1.0))
766 _test_generator(N, gammavariate, (0.1, 2.0))
767 _test_generator(N, gammavariate, (0.5, 1.0))
768 _test_generator(N, gammavariate, (0.9, 1.0))
769 _test_generator(N, gammavariate, (1.0, 1.0))
770 _test_generator(N, gammavariate, (2.0, 1.0))
771 _test_generator(N, gammavariate, (20.0, 1.0))
772 _test_generator(N, gammavariate, (200.0, 1.0))
773 _test_generator(N, gauss, (0.0, 1.0))
774 _test_generator(N, betavariate, (3.0, 3.0))
Tim Peterscd804102001-01-25 20:25:57 +0000775
Tim Peters715c4c42001-01-26 22:56:56 +0000776# Create one instance, seeded from current time, and export its methods
Raymond Hettinger40f62172002-12-29 23:03:38 +0000777# as module-level functions. The functions share state across all uses
778#(both in the user's code and in the Python libraries), but that's fine
779# for most programs and is easier for the casual user than making them
780# instantiate their own Random() instance.
781
Tim Petersd7b5e882001-01-25 03:36:26 +0000782_inst = Random()
783seed = _inst.seed
784random = _inst.random
785uniform = _inst.uniform
786randint = _inst.randint
787choice = _inst.choice
788randrange = _inst.randrange
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000789sample = _inst.sample
Tim Petersd7b5e882001-01-25 03:36:26 +0000790shuffle = _inst.shuffle
791normalvariate = _inst.normalvariate
792lognormvariate = _inst.lognormvariate
Tim Petersd7b5e882001-01-25 03:36:26 +0000793expovariate = _inst.expovariate
794vonmisesvariate = _inst.vonmisesvariate
795gammavariate = _inst.gammavariate
Tim Petersd7b5e882001-01-25 03:36:26 +0000796gauss = _inst.gauss
797betavariate = _inst.betavariate
798paretovariate = _inst.paretovariate
799weibullvariate = _inst.weibullvariate
800getstate = _inst.getstate
801setstate = _inst.setstate
Tim Petersd52269b2001-01-25 06:23:18 +0000802jumpahead = _inst.jumpahead
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000803getrandbits = _inst.getrandbits
Tim Petersd7b5e882001-01-25 03:36:26 +0000804
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000805if __name__ == '__main__':
Tim Petersd7b5e882001-01-25 03:36:26 +0000806 _test()