blob: 06990d243a59216d271dc64e5d7dfcd9ac9188d2 [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 Hettinger356a4592004-08-30 06:14:31 +000052 "getstate","setstate","jumpahead", "WichmannHill", "getrandbits",
53 "HardwareRandom"]
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
Raymond Hettinger356a4592004-08-30 06:14:31 +000061try:
62 from os import urandom as _urandom
63 from binascii import hexlify as _hexlify
64except ImportError:
65 _urandom = None
66else:
67 _tofloat = 2.0 ** (-7*8) # converts 7 byte integers to floats
68
69
Tim Petersd7b5e882001-01-25 03:36:26 +000070# Translated by Guido van Rossum from C source provided by
Raymond Hettinger40f62172002-12-29 23:03:38 +000071# Adrian Baddeley. Adapted by Raymond Hettinger for use with
72# the Mersenne Twister core generator.
Tim Petersd7b5e882001-01-25 03:36:26 +000073
Raymond Hettinger145a4a02003-01-07 10:25:55 +000074import _random
Raymond Hettinger40f62172002-12-29 23:03:38 +000075
Raymond Hettinger145a4a02003-01-07 10:25:55 +000076class Random(_random.Random):
Raymond Hettingerc32f0332002-05-23 19:44:49 +000077 """Random number generator base class used by bound module functions.
78
79 Used to instantiate instances of Random to get generators that don't
80 share state. Especially useful for multi-threaded programs, creating
81 a different instance of Random for each thread, and using the jumpahead()
82 method to ensure that the generated sequences seen by each thread don't
83 overlap.
84
85 Class Random can also be subclassed if you want to use a different basic
86 generator of your own devising: in that case, override the following
87 methods: random(), seed(), getstate(), setstate() and jumpahead().
Raymond Hettinger2f726e92003-10-05 09:09:15 +000088 Optionally, implement a getrandombits() method so that randrange()
89 can cover arbitrarily large ranges.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +000090
Raymond Hettingerc32f0332002-05-23 19:44:49 +000091 """
Tim Petersd7b5e882001-01-25 03:36:26 +000092
Raymond Hettinger40f62172002-12-29 23:03:38 +000093 VERSION = 2 # used by getstate/setstate
Tim Petersd7b5e882001-01-25 03:36:26 +000094
95 def __init__(self, x=None):
96 """Initialize an instance.
97
98 Optional argument x controls seeding, as for Random.seed().
99 """
100
101 self.seed(x)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000102 self.gauss_next = None
Tim Petersd7b5e882001-01-25 03:36:26 +0000103
Tim Peters0de88fc2001-02-01 04:59:18 +0000104 def seed(self, a=None):
105 """Initialize internal state from hashable object.
Tim Petersd7b5e882001-01-25 03:36:26 +0000106
Raymond Hettinger356a4592004-08-30 06:14:31 +0000107 None or no argument seeds from current time or from a hardware
108 randomness source if available.
Tim Peters0de88fc2001-02-01 04:59:18 +0000109
Tim Petersbcd725f2001-02-01 10:06:53 +0000110 If a is not None or an int or long, hash(a) is used instead.
Tim Petersd7b5e882001-01-25 03:36:26 +0000111 """
112
Raymond Hettinger3081d592003-08-09 18:30:57 +0000113 if a is None:
Raymond Hettinger356a4592004-08-30 06:14:31 +0000114 if _urandom is None:
115 import time
116 a = long(time.time() * 256) # use fractional seconds
117 else:
118 a = long(_hexlify(_urandom(16)), 16)
119
Raymond Hettinger145a4a02003-01-07 10:25:55 +0000120 super(Random, self).seed(a)
Tim Peters46c04e12002-05-05 20:40:00 +0000121 self.gauss_next = None
122
Tim Peterscd804102001-01-25 20:25:57 +0000123 def getstate(self):
124 """Return internal state; can be passed to setstate() later."""
Raymond Hettinger145a4a02003-01-07 10:25:55 +0000125 return self.VERSION, super(Random, self).getstate(), self.gauss_next
Tim Peterscd804102001-01-25 20:25:57 +0000126
127 def setstate(self, state):
128 """Restore internal state from object returned by getstate()."""
129 version = state[0]
Raymond Hettinger40f62172002-12-29 23:03:38 +0000130 if version == 2:
131 version, internalstate, self.gauss_next = state
Raymond Hettinger145a4a02003-01-07 10:25:55 +0000132 super(Random, self).setstate(internalstate)
Tim Peterscd804102001-01-25 20:25:57 +0000133 else:
134 raise ValueError("state with version %s passed to "
135 "Random.setstate() of version %s" %
136 (version, self.VERSION))
137
Tim Peterscd804102001-01-25 20:25:57 +0000138## ---- Methods below this point do not need to be overridden when
139## ---- subclassing for the purpose of using a different core generator.
140
141## -------------------- pickle support -------------------
142
143 def __getstate__(self): # for pickle
144 return self.getstate()
145
146 def __setstate__(self, state): # for pickle
147 self.setstate(state)
148
Raymond Hettinger5f078ff2003-06-24 20:29:04 +0000149 def __reduce__(self):
150 return self.__class__, (), self.getstate()
151
Tim Peterscd804102001-01-25 20:25:57 +0000152## -------------------- integer methods -------------------
153
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000154 def randrange(self, start, stop=None, step=1, int=int, default=None,
155 maxwidth=1L<<BPF):
Tim Petersd7b5e882001-01-25 03:36:26 +0000156 """Choose a random item from range(start, stop[, step]).
157
158 This fixes the problem with randint() which includes the
159 endpoint; in Python this is usually not what you want.
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000160 Do not supply the 'int', 'default', and 'maxwidth' arguments.
Tim Petersd7b5e882001-01-25 03:36:26 +0000161 """
162
163 # This code is a bit messy to make it fast for the
Tim Peters9146f272002-08-16 03:41:39 +0000164 # common case while still doing adequate error checking.
Tim Petersd7b5e882001-01-25 03:36:26 +0000165 istart = int(start)
166 if istart != start:
167 raise ValueError, "non-integer arg 1 for randrange()"
168 if stop is default:
169 if istart > 0:
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000170 if istart >= maxwidth:
171 return self._randbelow(istart)
Tim Petersd7b5e882001-01-25 03:36:26 +0000172 return int(self.random() * istart)
173 raise ValueError, "empty range for randrange()"
Tim Peters9146f272002-08-16 03:41:39 +0000174
175 # stop argument supplied.
Tim Petersd7b5e882001-01-25 03:36:26 +0000176 istop = int(stop)
177 if istop != stop:
178 raise ValueError, "non-integer stop for randrange()"
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000179 width = istop - istart
180 if step == 1 and width > 0:
Tim Peters76ca1d42003-06-19 03:46:46 +0000181 # Note that
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000182 # int(istart + self.random()*width)
Tim Peters76ca1d42003-06-19 03:46:46 +0000183 # instead would be incorrect. For example, consider istart
184 # = -2 and istop = 0. Then the guts would be in
185 # -2.0 to 0.0 exclusive on both ends (ignoring that random()
186 # might return 0.0), and because int() truncates toward 0, the
187 # final result would be -1 or 0 (instead of -2 or -1).
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000188 # istart + int(self.random()*width)
Tim Peters76ca1d42003-06-19 03:46:46 +0000189 # would also be incorrect, for a subtler reason: the RHS
190 # can return a long, and then randrange() would also return
191 # a long, but we're supposed to return an int (for backward
192 # compatibility).
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000193
194 if width >= maxwidth:
Tim Peters58eb11c2004-01-18 20:29:55 +0000195 return int(istart + self._randbelow(width))
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000196 return int(istart + int(self.random()*width))
Tim Petersd7b5e882001-01-25 03:36:26 +0000197 if step == 1:
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000198 raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width)
Tim Peters9146f272002-08-16 03:41:39 +0000199
200 # Non-unit step argument supplied.
Tim Petersd7b5e882001-01-25 03:36:26 +0000201 istep = int(step)
202 if istep != step:
203 raise ValueError, "non-integer step for randrange()"
204 if istep > 0:
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000205 n = (width + istep - 1) / istep
Tim Petersd7b5e882001-01-25 03:36:26 +0000206 elif istep < 0:
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000207 n = (width + istep + 1) / istep
Tim Petersd7b5e882001-01-25 03:36:26 +0000208 else:
209 raise ValueError, "zero step for randrange()"
210
211 if n <= 0:
212 raise ValueError, "empty range for randrange()"
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000213
214 if n >= maxwidth:
215 return istart + self._randbelow(n)
Tim Petersd7b5e882001-01-25 03:36:26 +0000216 return istart + istep*int(self.random() * n)
217
218 def randint(self, a, b):
Tim Peterscd804102001-01-25 20:25:57 +0000219 """Return random integer in range [a, b], including both end points.
Tim Petersd7b5e882001-01-25 03:36:26 +0000220 """
221
222 return self.randrange(a, b+1)
223
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000224 def _randbelow(self, n, _log=_log, int=int, _maxwidth=1L<<BPF,
225 _Method=_MethodType, _BuiltinMethod=_BuiltinMethodType):
226 """Return a random int in the range [0,n)
227
228 Handles the case where n has more bits than returned
229 by a single call to the underlying generator.
230 """
231
232 try:
233 getrandbits = self.getrandbits
234 except AttributeError:
235 pass
236 else:
237 # Only call self.getrandbits if the original random() builtin method
238 # has not been overridden or if a new getrandbits() was supplied.
239 # This assures that the two methods correspond.
240 if type(self.random) is _BuiltinMethod or type(getrandbits) is _Method:
241 k = int(1.00001 + _log(n-1, 2.0)) # 2**k > n-1 > 2**(k-2)
242 r = getrandbits(k)
243 while r >= n:
244 r = getrandbits(k)
245 return r
246 if n >= _maxwidth:
247 _warn("Underlying random() generator does not supply \n"
248 "enough bits to choose from a population range this large")
249 return int(self.random() * n)
250
Tim Peterscd804102001-01-25 20:25:57 +0000251## -------------------- sequence methods -------------------
252
Tim Petersd7b5e882001-01-25 03:36:26 +0000253 def choice(self, seq):
254 """Choose a random element from a non-empty sequence."""
Raymond Hettinger5dae5052004-06-07 02:07:15 +0000255 return seq[int(self.random() * len(seq))] # raises IndexError if seq is empty
Tim Petersd7b5e882001-01-25 03:36:26 +0000256
257 def shuffle(self, x, random=None, int=int):
258 """x, random=random.random -> shuffle list x in place; return None.
259
260 Optional arg random is a 0-argument function returning a random
261 float in [0.0, 1.0); by default, the standard random.random.
262
263 Note that for even rather small len(x), the total number of
264 permutations of x is larger than the period of most random number
265 generators; this implies that "most" permutations of a long
266 sequence can never be generated.
267 """
268
269 if random is None:
270 random = self.random
Raymond Hettinger85c20a42003-11-06 14:06:48 +0000271 for i in reversed(xrange(1, len(x))):
Tim Peterscd804102001-01-25 20:25:57 +0000272 # pick an element in x[:i+1] with which to exchange x[i]
Tim Petersd7b5e882001-01-25 03:36:26 +0000273 j = int(random() * (i+1))
274 x[i], x[j] = x[j], x[i]
275
Raymond Hettingerfdbe5222003-06-13 07:01:51 +0000276 def sample(self, population, k):
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000277 """Chooses k unique random elements from a population sequence.
278
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000279 Returns a new list containing elements from the population while
280 leaving the original population unchanged. The resulting list is
281 in selection order so that all sub-slices will also be valid random
282 samples. This allows raffle winners (the sample) to be partitioned
283 into grand prize and second place winners (the subslices).
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000284
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000285 Members of the population need not be hashable or unique. If the
286 population contains repeats, then each occurrence is a possible
287 selection in the sample.
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000288
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000289 To choose a sample in a range of integers, use xrange as an argument.
290 This is especially fast and space efficient for sampling from a
291 large population: sample(xrange(10000000), 60)
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000292 """
293
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000294 # Sampling without replacement entails tracking either potential
Raymond Hettinger8b9aa8d2003-01-04 05:20:33 +0000295 # selections (the pool) in a list or previous selections in a
296 # dictionary.
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000297
Jeremy Hylton2b55d352004-02-23 17:27:57 +0000298 # When the number of selections is small compared to the
299 # population, then tracking selections is efficient, requiring
300 # only a small dictionary and an occasional reselection. For
301 # a larger number of selections, the pool tracking method is
302 # preferred since the list takes less space than the
303 # dictionary and it doesn't suffer from frequent reselections.
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000304
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000305 n = len(population)
306 if not 0 <= k <= n:
307 raise ValueError, "sample larger than population"
Raymond Hettinger8b9aa8d2003-01-04 05:20:33 +0000308 random = self.random
Raymond Hettingerfdbe5222003-06-13 07:01:51 +0000309 _int = int
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000310 result = [None] * k
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000311 if n < 6 * k: # if n len list takes less space than a k len dict
Raymond Hettinger311f4192002-11-18 09:01:24 +0000312 pool = list(population)
313 for i in xrange(k): # invariant: non-selected at [0,n-i)
Raymond Hettingerfdbe5222003-06-13 07:01:51 +0000314 j = _int(random() * (n-i))
Raymond Hettinger311f4192002-11-18 09:01:24 +0000315 result[i] = pool[j]
Raymond Hettinger8b9aa8d2003-01-04 05:20:33 +0000316 pool[j] = pool[n-i-1] # move non-selected item into vacancy
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000317 else:
Raymond Hettinger66d09f12003-09-06 04:25:54 +0000318 try:
319 n > 0 and (population[0], population[n//2], population[n-1])
320 except (TypeError, KeyError): # handle sets and dictionaries
321 population = tuple(population)
Raymond Hettinger311f4192002-11-18 09:01:24 +0000322 selected = {}
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000323 for i in xrange(k):
Raymond Hettingerfdbe5222003-06-13 07:01:51 +0000324 j = _int(random() * n)
Raymond Hettinger311f4192002-11-18 09:01:24 +0000325 while j in selected:
Raymond Hettingerfdbe5222003-06-13 07:01:51 +0000326 j = _int(random() * n)
Raymond Hettingerc0b40342002-11-13 15:26:37 +0000327 result[i] = selected[j] = population[j]
Raymond Hettinger311f4192002-11-18 09:01:24 +0000328 return result
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000329
Tim Peterscd804102001-01-25 20:25:57 +0000330## -------------------- real-valued distributions -------------------
331
332## -------------------- uniform distribution -------------------
Tim Petersd7b5e882001-01-25 03:36:26 +0000333
334 def uniform(self, a, b):
335 """Get a random number in the range [a, b)."""
336 return a + (b-a) * self.random()
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000337
Tim Peterscd804102001-01-25 20:25:57 +0000338## -------------------- normal distribution --------------------
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000339
Tim Petersd7b5e882001-01-25 03:36:26 +0000340 def normalvariate(self, mu, sigma):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000341 """Normal distribution.
342
343 mu is the mean, and sigma is the standard deviation.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000344
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000345 """
Tim Petersd7b5e882001-01-25 03:36:26 +0000346 # mu = mean, sigma = standard deviation
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000347
Tim Petersd7b5e882001-01-25 03:36:26 +0000348 # Uses Kinderman and Monahan method. Reference: Kinderman,
349 # A.J. and Monahan, J.F., "Computer generation of random
350 # variables using the ratio of uniform deviates", ACM Trans
351 # Math Software, 3, (1977), pp257-260.
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000352
Tim Petersd7b5e882001-01-25 03:36:26 +0000353 random = self.random
Raymond Hettinger311f4192002-11-18 09:01:24 +0000354 while True:
Tim Peters0c9886d2001-01-15 01:18:21 +0000355 u1 = random()
Raymond Hettinger73ced7e2003-01-04 09:26:32 +0000356 u2 = 1.0 - random()
Tim Petersd7b5e882001-01-25 03:36:26 +0000357 z = NV_MAGICCONST*(u1-0.5)/u2
358 zz = z*z/4.0
359 if zz <= -_log(u2):
360 break
361 return mu + z*sigma
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000362
Tim Peterscd804102001-01-25 20:25:57 +0000363## -------------------- lognormal distribution --------------------
Tim Petersd7b5e882001-01-25 03:36:26 +0000364
365 def lognormvariate(self, mu, sigma):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000366 """Log normal distribution.
367
368 If you take the natural logarithm of this distribution, you'll get a
369 normal distribution with mean mu and standard deviation sigma.
370 mu can have any value, and sigma must be greater than zero.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000371
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000372 """
Tim Petersd7b5e882001-01-25 03:36:26 +0000373 return _exp(self.normalvariate(mu, sigma))
374
Tim Peterscd804102001-01-25 20:25:57 +0000375## -------------------- exponential distribution --------------------
Tim Petersd7b5e882001-01-25 03:36:26 +0000376
377 def expovariate(self, lambd):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000378 """Exponential distribution.
379
380 lambd is 1.0 divided by the desired mean. (The parameter would be
381 called "lambda", but that is a reserved word in Python.) Returned
382 values range from 0 to positive infinity.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000383
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000384 """
Tim Petersd7b5e882001-01-25 03:36:26 +0000385 # lambd: rate lambd = 1/mean
386 # ('lambda' is a Python reserved word)
387
388 random = self.random
Tim Peters0c9886d2001-01-15 01:18:21 +0000389 u = random()
390 while u <= 1e-7:
391 u = random()
Tim Petersd7b5e882001-01-25 03:36:26 +0000392 return -_log(u)/lambd
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000393
Tim Peterscd804102001-01-25 20:25:57 +0000394## -------------------- von Mises distribution --------------------
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000395
Tim Petersd7b5e882001-01-25 03:36:26 +0000396 def vonmisesvariate(self, mu, kappa):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000397 """Circular data distribution.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000398
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000399 mu is the mean angle, expressed in radians between 0 and 2*pi, and
400 kappa is the concentration parameter, which must be greater than or
401 equal to zero. If kappa is equal to zero, this distribution reduces
402 to a uniform random angle over the range 0 to 2*pi.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000403
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000404 """
Tim Petersd7b5e882001-01-25 03:36:26 +0000405 # mu: mean angle (in radians between 0 and 2*pi)
406 # kappa: concentration parameter kappa (>= 0)
407 # if kappa = 0 generate uniform random angle
408
409 # Based upon an algorithm published in: Fisher, N.I.,
410 # "Statistical Analysis of Circular Data", Cambridge
411 # University Press, 1993.
412
413 # Thanks to Magnus Kessler for a correction to the
414 # implementation of step 4.
415
416 random = self.random
417 if kappa <= 1e-6:
418 return TWOPI * random()
419
420 a = 1.0 + _sqrt(1.0 + 4.0 * kappa * kappa)
421 b = (a - _sqrt(2.0 * a))/(2.0 * kappa)
422 r = (1.0 + b * b)/(2.0 * b)
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000423
Raymond Hettinger311f4192002-11-18 09:01:24 +0000424 while True:
Tim Peters0c9886d2001-01-15 01:18:21 +0000425 u1 = random()
Tim Petersd7b5e882001-01-25 03:36:26 +0000426
427 z = _cos(_pi * u1)
428 f = (1.0 + r * z)/(r + z)
429 c = kappa * (r - f)
430
431 u2 = random()
432
433 if not (u2 >= c * (2.0 - c) and u2 > c * _exp(1.0 - c)):
Tim Peters0c9886d2001-01-15 01:18:21 +0000434 break
Tim Petersd7b5e882001-01-25 03:36:26 +0000435
436 u3 = random()
437 if u3 > 0.5:
438 theta = (mu % TWOPI) + _acos(f)
439 else:
440 theta = (mu % TWOPI) - _acos(f)
441
442 return theta
443
Tim Peterscd804102001-01-25 20:25:57 +0000444## -------------------- gamma distribution --------------------
Tim Petersd7b5e882001-01-25 03:36:26 +0000445
446 def gammavariate(self, alpha, beta):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000447 """Gamma distribution. Not the gamma function!
448
449 Conditions on the parameters are alpha > 0 and beta > 0.
450
451 """
Tim Peters8ac14952002-05-23 15:15:30 +0000452
Raymond Hettingerb760efb2002-05-14 06:40:34 +0000453 # alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2
Tim Peters8ac14952002-05-23 15:15:30 +0000454
Guido van Rossum570764d2002-05-14 14:08:12 +0000455 # Warning: a few older sources define the gamma distribution in terms
456 # of alpha > -1.0
457 if alpha <= 0.0 or beta <= 0.0:
458 raise ValueError, 'gammavariate: alpha and beta must be > 0.0'
Tim Peters8ac14952002-05-23 15:15:30 +0000459
Tim Petersd7b5e882001-01-25 03:36:26 +0000460 random = self.random
Tim Petersd7b5e882001-01-25 03:36:26 +0000461 if alpha > 1.0:
462
463 # Uses R.C.H. Cheng, "The generation of Gamma
464 # variables with non-integral shape parameters",
465 # Applied Statistics, (1977), 26, No. 1, p71-74
466
Raymond Hettingerca6cdc22002-05-13 23:40:14 +0000467 ainv = _sqrt(2.0 * alpha - 1.0)
468 bbb = alpha - LOG4
469 ccc = alpha + ainv
Tim Peters8ac14952002-05-23 15:15:30 +0000470
Raymond Hettinger311f4192002-11-18 09:01:24 +0000471 while True:
Tim Petersd7b5e882001-01-25 03:36:26 +0000472 u1 = random()
Raymond Hettinger73ced7e2003-01-04 09:26:32 +0000473 if not 1e-7 < u1 < .9999999:
474 continue
475 u2 = 1.0 - random()
Tim Petersd7b5e882001-01-25 03:36:26 +0000476 v = _log(u1/(1.0-u1))/ainv
477 x = alpha*_exp(v)
478 z = u1*u1*u2
479 r = bbb+ccc*v-x
480 if r + SG_MAGICCONST - 4.5*z >= 0.0 or r >= _log(z):
Raymond Hettingerb760efb2002-05-14 06:40:34 +0000481 return x * beta
Tim Petersd7b5e882001-01-25 03:36:26 +0000482
483 elif alpha == 1.0:
484 # expovariate(1)
485 u = random()
486 while u <= 1e-7:
487 u = random()
Raymond Hettingerb760efb2002-05-14 06:40:34 +0000488 return -_log(u) * beta
Tim Petersd7b5e882001-01-25 03:36:26 +0000489
490 else: # alpha is between 0 and 1 (exclusive)
491
492 # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
493
Raymond Hettinger311f4192002-11-18 09:01:24 +0000494 while True:
Tim Petersd7b5e882001-01-25 03:36:26 +0000495 u = random()
496 b = (_e + alpha)/_e
497 p = b*u
498 if p <= 1.0:
499 x = pow(p, 1.0/alpha)
500 else:
501 # p > 1
502 x = -_log((b-p)/alpha)
503 u1 = random()
504 if not (((p <= 1.0) and (u1 > _exp(-x))) or
505 ((p > 1) and (u1 > pow(x, alpha - 1.0)))):
506 break
Raymond Hettingerb760efb2002-05-14 06:40:34 +0000507 return x * beta
508
Tim Peterscd804102001-01-25 20:25:57 +0000509## -------------------- Gauss (faster alternative) --------------------
Guido van Rossum95bfcda1994-03-09 14:21:05 +0000510
Tim Petersd7b5e882001-01-25 03:36:26 +0000511 def gauss(self, mu, sigma):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000512 """Gaussian distribution.
513
514 mu is the mean, and sigma is the standard deviation. This is
515 slightly faster than the normalvariate() function.
516
517 Not thread-safe without a lock around calls.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000518
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000519 """
Guido van Rossumcc32ac91994-03-15 16:10:24 +0000520
Tim Petersd7b5e882001-01-25 03:36:26 +0000521 # When x and y are two variables from [0, 1), uniformly
522 # distributed, then
523 #
524 # cos(2*pi*x)*sqrt(-2*log(1-y))
525 # sin(2*pi*x)*sqrt(-2*log(1-y))
526 #
527 # are two *independent* variables with normal distribution
528 # (mu = 0, sigma = 1).
529 # (Lambert Meertens)
530 # (corrected version; bug discovered by Mike Miller, fixed by LM)
Guido van Rossumcc32ac91994-03-15 16:10:24 +0000531
Tim Petersd7b5e882001-01-25 03:36:26 +0000532 # Multithreading note: When two threads call this function
533 # simultaneously, it is possible that they will receive the
534 # same return value. The window is very small though. To
535 # avoid this, you have to use a lock around all calls. (I
536 # didn't want to slow this down in the serial case by using a
537 # lock here.)
Guido van Rossumd03e1191998-05-29 17:51:31 +0000538
Tim Petersd7b5e882001-01-25 03:36:26 +0000539 random = self.random
540 z = self.gauss_next
541 self.gauss_next = None
542 if z is None:
543 x2pi = random() * TWOPI
544 g2rad = _sqrt(-2.0 * _log(1.0 - random()))
545 z = _cos(x2pi) * g2rad
546 self.gauss_next = _sin(x2pi) * g2rad
Guido van Rossumcc32ac91994-03-15 16:10:24 +0000547
Tim Petersd7b5e882001-01-25 03:36:26 +0000548 return mu + z*sigma
Guido van Rossum95bfcda1994-03-09 14:21:05 +0000549
Tim Peterscd804102001-01-25 20:25:57 +0000550## -------------------- beta --------------------
Tim Peters85e2e472001-01-26 06:49:56 +0000551## See
552## http://sourceforge.net/bugs/?func=detailbug&bug_id=130030&group_id=5470
553## for Ivan Frohne's insightful analysis of why the original implementation:
554##
555## def betavariate(self, alpha, beta):
556## # Discrete Event Simulation in C, pp 87-88.
557##
558## y = self.expovariate(alpha)
559## z = self.expovariate(1.0/beta)
560## return z/(y+z)
561##
562## was dead wrong, and how it probably got that way.
Guido van Rossum95bfcda1994-03-09 14:21:05 +0000563
Tim Petersd7b5e882001-01-25 03:36:26 +0000564 def betavariate(self, alpha, beta):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000565 """Beta distribution.
566
567 Conditions on the parameters are alpha > -1 and beta} > -1.
568 Returned values range between 0 and 1.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000569
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000570 """
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000571
Tim Peters85e2e472001-01-26 06:49:56 +0000572 # This version due to Janne Sinkkonen, and matches all the std
573 # texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution").
574 y = self.gammavariate(alpha, 1.)
575 if y == 0:
576 return 0.0
577 else:
578 return y / (y + self.gammavariate(beta, 1.))
Guido van Rossum95bfcda1994-03-09 14:21:05 +0000579
Tim Peterscd804102001-01-25 20:25:57 +0000580## -------------------- Pareto --------------------
Guido van Rossumcf4559a1997-12-02 02:47:39 +0000581
Tim Petersd7b5e882001-01-25 03:36:26 +0000582 def paretovariate(self, alpha):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000583 """Pareto distribution. alpha is the shape parameter."""
Tim Petersd7b5e882001-01-25 03:36:26 +0000584 # Jain, pg. 495
Guido van Rossumcf4559a1997-12-02 02:47:39 +0000585
Raymond Hettinger73ced7e2003-01-04 09:26:32 +0000586 u = 1.0 - self.random()
Tim Petersd7b5e882001-01-25 03:36:26 +0000587 return 1.0 / pow(u, 1.0/alpha)
Guido van Rossumcf4559a1997-12-02 02:47:39 +0000588
Tim Peterscd804102001-01-25 20:25:57 +0000589## -------------------- Weibull --------------------
Guido van Rossumcf4559a1997-12-02 02:47:39 +0000590
Tim Petersd7b5e882001-01-25 03:36:26 +0000591 def weibullvariate(self, alpha, beta):
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000592 """Weibull distribution.
593
594 alpha is the scale parameter and beta is the shape parameter.
Raymond Hettingeref4d4bd2002-05-23 23:58:17 +0000595
Raymond Hettingerc32f0332002-05-23 19:44:49 +0000596 """
Tim Petersd7b5e882001-01-25 03:36:26 +0000597 # Jain, pg. 499; bug fix courtesy Bill Arms
Guido van Rossumcf4559a1997-12-02 02:47:39 +0000598
Raymond Hettinger73ced7e2003-01-04 09:26:32 +0000599 u = 1.0 - self.random()
Tim Petersd7b5e882001-01-25 03:36:26 +0000600 return alpha * pow(-_log(u), 1.0/beta)
Guido van Rossum6c395ba1999-08-18 13:53:28 +0000601
Raymond Hettinger40f62172002-12-29 23:03:38 +0000602## -------------------- Wichmann-Hill -------------------
603
604class WichmannHill(Random):
605
606 VERSION = 1 # used by getstate/setstate
607
608 def seed(self, a=None):
609 """Initialize internal state from hashable object.
610
Raymond Hettinger356a4592004-08-30 06:14:31 +0000611 None or no argument seeds from current time or from a hardware
612 randomness source if available.
Raymond Hettinger40f62172002-12-29 23:03:38 +0000613
614 If a is not None or an int or long, hash(a) is used instead.
615
616 If a is an int or long, a is used directly. Distinct values between
617 0 and 27814431486575L inclusive are guaranteed to yield distinct
618 internal states (this guarantee is specific to the default
619 Wichmann-Hill generator).
620 """
621
622 if a is None:
Raymond Hettinger356a4592004-08-30 06:14:31 +0000623 if _urandom is None:
624 import time
625 a = long(time.time() * 256) # use fractional seconds
626 else:
627 a = long(_hexlify(_urandom(16)), 16)
Raymond Hettinger40f62172002-12-29 23:03:38 +0000628
629 if not isinstance(a, (int, long)):
630 a = hash(a)
631
632 a, x = divmod(a, 30268)
633 a, y = divmod(a, 30306)
634 a, z = divmod(a, 30322)
635 self._seed = int(x)+1, int(y)+1, int(z)+1
636
637 self.gauss_next = None
638
639 def random(self):
640 """Get the next random number in the range [0.0, 1.0)."""
641
642 # Wichman-Hill random number generator.
643 #
644 # Wichmann, B. A. & Hill, I. D. (1982)
645 # Algorithm AS 183:
646 # An efficient and portable pseudo-random number generator
647 # Applied Statistics 31 (1982) 188-190
648 #
649 # see also:
650 # Correction to Algorithm AS 183
651 # Applied Statistics 33 (1984) 123
652 #
653 # McLeod, A. I. (1985)
654 # A remark on Algorithm AS 183
655 # Applied Statistics 34 (1985),198-200
656
657 # This part is thread-unsafe:
658 # BEGIN CRITICAL SECTION
659 x, y, z = self._seed
660 x = (171 * x) % 30269
661 y = (172 * y) % 30307
662 z = (170 * z) % 30323
663 self._seed = x, y, z
664 # END CRITICAL SECTION
665
666 # Note: on a platform using IEEE-754 double arithmetic, this can
667 # never return 0.0 (asserted by Tim; proof too long for a comment).
668 return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
669
670 def getstate(self):
671 """Return internal state; can be passed to setstate() later."""
672 return self.VERSION, self._seed, self.gauss_next
673
674 def setstate(self, state):
675 """Restore internal state from object returned by getstate()."""
676 version = state[0]
677 if version == 1:
678 version, self._seed, self.gauss_next = state
679 else:
680 raise ValueError("state with version %s passed to "
681 "Random.setstate() of version %s" %
682 (version, self.VERSION))
683
684 def jumpahead(self, n):
685 """Act as if n calls to random() were made, but quickly.
686
687 n is an int, greater than or equal to 0.
688
689 Example use: If you have 2 threads and know that each will
690 consume no more than a million random numbers, create two Random
691 objects r1 and r2, then do
692 r2.setstate(r1.getstate())
693 r2.jumpahead(1000000)
694 Then r1 and r2 will use guaranteed-disjoint segments of the full
695 period.
696 """
697
698 if not n >= 0:
699 raise ValueError("n must be >= 0")
700 x, y, z = self._seed
701 x = int(x * pow(171, n, 30269)) % 30269
702 y = int(y * pow(172, n, 30307)) % 30307
703 z = int(z * pow(170, n, 30323)) % 30323
704 self._seed = x, y, z
705
706 def __whseed(self, x=0, y=0, z=0):
707 """Set the Wichmann-Hill seed from (x, y, z).
708
709 These must be integers in the range [0, 256).
710 """
711
712 if not type(x) == type(y) == type(z) == int:
713 raise TypeError('seeds must be integers')
714 if not (0 <= x < 256 and 0 <= y < 256 and 0 <= z < 256):
715 raise ValueError('seeds must be in range(0, 256)')
716 if 0 == x == y == z:
717 # Initialize from current time
718 import time
719 t = long(time.time() * 256)
720 t = int((t&0xffffff) ^ (t>>24))
721 t, x = divmod(t, 256)
722 t, y = divmod(t, 256)
723 t, z = divmod(t, 256)
724 # Zero is a poor seed, so substitute 1
725 self._seed = (x or 1, y or 1, z or 1)
726
727 self.gauss_next = None
728
729 def whseed(self, a=None):
730 """Seed from hashable object's hash code.
731
732 None or no argument seeds from current time. It is not guaranteed
733 that objects with distinct hash codes lead to distinct internal
734 states.
735
736 This is obsolete, provided for compatibility with the seed routine
737 used prior to Python 2.1. Use the .seed() method instead.
738 """
739
740 if a is None:
741 self.__whseed()
742 return
743 a = hash(a)
744 a, x = divmod(a, 256)
745 a, y = divmod(a, 256)
746 a, z = divmod(a, 256)
747 x = (x + a) % 256 or 1
748 y = (y + a) % 256 or 1
749 z = (z + a) % 256 or 1
750 self.__whseed(x, y, z)
751
Raymond Hettinger356a4592004-08-30 06:14:31 +0000752## -------------------- Hardware Random Source -------------------
753
754class HardwareRandom(Random):
755 """Alternate random number generator using hardware sources.
756
757 Not available on all systems (see os.urandom() for details).
758 """
759
760 def random(self):
761 """Get the next random number in the range [0.0, 1.0)."""
762 if _urandom is None:
763 raise NotImplementedError('Cannot find hardware entropy source')
764 return long(_hexlify(_urandom(7)), 16) * _tofloat
765
766 def getrandbits(self, k):
767 """getrandbits(k) -> x. Generates a long int with k random bits."""
768 if _urandom is None:
769 raise NotImplementedError('Cannot find hardware entropy source')
770 if k <= 0:
771 raise ValueError('number of bits must be greater than zero')
772 if k != int(k):
773 raise TypeError('number of bits should be an integer')
774 bytes = (k + 7) // 8 # bits / 8 and rounded up
775 x = long(_hexlify(_urandom(bytes)), 16)
776 return x >> (bytes * 8 - k) # trim excess bits
777
778 def _stub(self, *args, **kwds):
779 "Stub method. Not used for a hardware random number generator."
780 return None
781 seed = jumpahead = _stub
782
783 def _notimplemented(self, *args, **kwds):
784 "Method should not be called for a hardware random number generator."
785 raise NotImplementedError('Hardware entropy source does not have state.')
786 getstate = setstate = _notimplemented
787
Tim Peterscd804102001-01-25 20:25:57 +0000788## -------------------- test program --------------------
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000789
Raymond Hettinger62297132003-08-30 01:24:19 +0000790def _test_generator(n, func, args):
Tim Peters0c9886d2001-01-15 01:18:21 +0000791 import time
Raymond Hettinger62297132003-08-30 01:24:19 +0000792 print n, 'times', func.__name__
Raymond Hettingerb98154e2003-05-24 17:26:02 +0000793 total = 0.0
Tim Peters0c9886d2001-01-15 01:18:21 +0000794 sqsum = 0.0
795 smallest = 1e10
796 largest = -1e10
797 t0 = time.time()
798 for i in range(n):
Raymond Hettinger62297132003-08-30 01:24:19 +0000799 x = func(*args)
Raymond Hettingerb98154e2003-05-24 17:26:02 +0000800 total += x
Tim Peters0c9886d2001-01-15 01:18:21 +0000801 sqsum = sqsum + x*x
802 smallest = min(x, smallest)
803 largest = max(x, largest)
804 t1 = time.time()
805 print round(t1-t0, 3), 'sec,',
Raymond Hettingerb98154e2003-05-24 17:26:02 +0000806 avg = total/n
Tim Petersd7b5e882001-01-25 03:36:26 +0000807 stddev = _sqrt(sqsum/n - avg*avg)
Tim Peters0c9886d2001-01-15 01:18:21 +0000808 print 'avg %g, stddev %g, min %g, max %g' % \
809 (avg, stddev, smallest, largest)
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000810
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000811
812def _test(N=2000):
Raymond Hettinger62297132003-08-30 01:24:19 +0000813 _test_generator(N, random, ())
814 _test_generator(N, normalvariate, (0.0, 1.0))
815 _test_generator(N, lognormvariate, (0.0, 1.0))
816 _test_generator(N, vonmisesvariate, (0.0, 1.0))
817 _test_generator(N, gammavariate, (0.01, 1.0))
818 _test_generator(N, gammavariate, (0.1, 1.0))
819 _test_generator(N, gammavariate, (0.1, 2.0))
820 _test_generator(N, gammavariate, (0.5, 1.0))
821 _test_generator(N, gammavariate, (0.9, 1.0))
822 _test_generator(N, gammavariate, (1.0, 1.0))
823 _test_generator(N, gammavariate, (2.0, 1.0))
824 _test_generator(N, gammavariate, (20.0, 1.0))
825 _test_generator(N, gammavariate, (200.0, 1.0))
826 _test_generator(N, gauss, (0.0, 1.0))
827 _test_generator(N, betavariate, (3.0, 3.0))
Tim Peterscd804102001-01-25 20:25:57 +0000828
Tim Peters715c4c42001-01-26 22:56:56 +0000829# Create one instance, seeded from current time, and export its methods
Raymond Hettinger40f62172002-12-29 23:03:38 +0000830# as module-level functions. The functions share state across all uses
831#(both in the user's code and in the Python libraries), but that's fine
832# for most programs and is easier for the casual user than making them
833# instantiate their own Random() instance.
834
Tim Petersd7b5e882001-01-25 03:36:26 +0000835_inst = Random()
836seed = _inst.seed
837random = _inst.random
838uniform = _inst.uniform
839randint = _inst.randint
840choice = _inst.choice
841randrange = _inst.randrange
Raymond Hettingerf24eb352002-11-12 17:41:57 +0000842sample = _inst.sample
Tim Petersd7b5e882001-01-25 03:36:26 +0000843shuffle = _inst.shuffle
844normalvariate = _inst.normalvariate
845lognormvariate = _inst.lognormvariate
Tim Petersd7b5e882001-01-25 03:36:26 +0000846expovariate = _inst.expovariate
847vonmisesvariate = _inst.vonmisesvariate
848gammavariate = _inst.gammavariate
Tim Petersd7b5e882001-01-25 03:36:26 +0000849gauss = _inst.gauss
850betavariate = _inst.betavariate
851paretovariate = _inst.paretovariate
852weibullvariate = _inst.weibullvariate
853getstate = _inst.getstate
854setstate = _inst.setstate
Tim Petersd52269b2001-01-25 06:23:18 +0000855jumpahead = _inst.jumpahead
Raymond Hettinger2f726e92003-10-05 09:09:15 +0000856getrandbits = _inst.getrandbits
Tim Petersd7b5e882001-01-25 03:36:26 +0000857
Guido van Rossumff03b1a1994-03-09 12:55:02 +0000858if __name__ == '__main__':
Tim Petersd7b5e882001-01-25 03:36:26 +0000859 _test()