1 """Random variable generators.
2
3 integers
4 --------
5 uniform within range
6
7 sequences
8 ---------
9 pick random element
10 pick random sample
11 generate random permutation
12
13 distributions on the real line:
14 ------------------------------
15 uniform
16 normal (Gaussian)
17 lognormal
18 negative exponential
19 gamma
20 beta
21 pareto
22 Weibull
23
24 distributions on the circle (angles 0 to 2pi)
25 ---------------------------------------------
26 circular uniform
27 von Mises
28
29 General notes on the underlying Mersenne Twister core generator:
30
31 * 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 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.
38
39 """
40
41 from warnings import warn as _warn
42 from types import MethodType as _MethodType, BuiltinMethodType as _BuiltinMethodType
43 from math import log as _log, exp as _exp, pi as _pi, e as _e, ceil as _ceil
44 from math import sqrt as _sqrt, acos as _acos, cos as _cos, sin as _sin
45 from os import urandom as _urandom
46 from binascii import hexlify as _hexlify
47
48 __all__ = ["Random","seed","random","uniform","randint","choice","sample",
49 "randrange","shuffle","normalvariate","lognormvariate",
50 "expovariate","vonmisesvariate","gammavariate",
51 "gauss","betavariate","paretovariate","weibullvariate",
52 "getstate","setstate","jumpahead", "WichmannHill", "getrandbits",
53 "SystemRandom"]
54
55 NV_MAGICCONST = 4 * _exp(-0.5)/_sqrt(2.0)
56 TWOPI = 2.0*_pi
57 LOG4 = _log(4.0)
58 SG_MAGICCONST = 1.0 + _log(4.5)
59 BPF = 53
60 RECIP_BPF = 2**-BPF
61
62
63
64
65
66
67 import _random
68
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().
81 Optionally, implement a getrandombits() method so that randrange()
82 can cover arbitrarily large ranges.
83
84 """
85
86 VERSION = 2
87
89 """Initialize an instance.
90
91 Optional argument x controls seeding, as for Random.seed().
92 """
93
94 self.seed(x)
95 self.gauss_next = None
96
98 """Initialize internal state from hashable object.
99
100 None or no argument seeds from current time or from an operating
101 system specific randomness source if available.
102
103 If a is not None or an int or long, hash(a) is used instead.
104 """
105
106 if a is None:
107 try:
108 a = long(_hexlify(_urandom(16)), 16)
109 except NotImplementedError:
110 import time
111 a = long(time.time() * 256)
112
113 super(Random, self).seed(a)
114 self.gauss_next = None
115
117 """Return internal state; can be passed to setstate() later."""
118 return self.VERSION, super(Random, self).getstate(), self.gauss_next
119
121 """Restore internal state from object returned by getstate()."""
122 version = state[0]
123 if version == 2:
124 version, internalstate, self.gauss_next = state
125 super(Random, self).setstate(internalstate)
126 else:
127 raise ValueError("state with version %s passed to "
128 "Random.setstate() of version %s" %
129 (version, self.VERSION))
130
131
132
133
134
135
137 return self.getstate()
138
140 self.setstate(state)
141
143 return self.__class__, (), self.getstate()
144
145
146
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.
153 Do not supply the 'int', 'default', and 'maxwidth' arguments.
154 """
155
156
157
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:
163 if istart >= maxwidth:
164 return self._randbelow(istart)
165 return int(self.random() * istart)
166 raise ValueError, "empty range for randrange()"
167
168
169 istop = int(stop)
170 if istop != stop:
171 raise ValueError, "non-integer stop for randrange()"
172 width = istop - istart
173 if step == 1 and width > 0:
174
175
176
177
178
179
180
181
182
183
184
185
186
187 if width >= maxwidth:
188 return int(istart + self._randbelow(width))
189 return int(istart + int(self.random()*width))
190 if step == 1:
191 raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart, istop, width)
192
193
194 istep = int(step)
195 if istep != step:
196 raise ValueError, "non-integer step for randrange()"
197 if istep > 0:
198 n = (width + istep - 1) // istep
199 elif istep < 0:
200 n = (width + istep + 1) // istep
201 else:
202 raise ValueError, "zero step for randrange()"
203
204 if n <= 0:
205 raise ValueError, "empty range for randrange()"
206
207 if n >= maxwidth:
208 return istart + istep*self._randbelow(n)
209 return istart + istep*int(self.random() * n)
210
212 """Return random integer in range [a, b], including both end points.
213 """
214
215 return self.randrange(a, b+1)
216
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
231
232
233 if type(self.random) is _BuiltinMethod or type(getrandbits) is _Method:
234 k = int(1.00001 + _log(n-1, 2.0))
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
244
245
247 """Choose a random element from a non-empty sequence."""
248 return seq[int(self.random() * len(seq))]
249
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.
255 """
256
257 if random is None:
258 random = self.random
259 for i in reversed(xrange(1, len(x))):
260
261 j = int(random() * (i+1))
262 x[i], x[j] = x[j], x[i]
263
264 - def sample(self, population, k):
265 """Chooses k unique random elements from a population sequence.
266
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).
272
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.
276
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)
280 """
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301 n = len(population)
302 if not 0 <= k <= n:
303 raise ValueError, "sample larger than population"
304 random = self.random
305 _int = int
306 result = [None] * k
307 setsize = 21
308 if k > 5:
309 setsize += 4 ** _ceil(_log(k * 3, 4))
310 if n <= setsize or hasattr(population, "keys"):
311
312
313 pool = list(population)
314 for i in xrange(k):
315 j = _int(random() * (n-i))
316 result[i] = pool[j]
317 pool[j] = pool[n-i-1]
318 else:
319 try:
320 selected = set()
321 selected_add = selected.add
322 for i in xrange(k):
323 j = _int(random() * n)
324 while j in selected:
325 j = _int(random() * n)
326 selected_add(j)
327 result[i] = population[j]
328 except (TypeError, KeyError):
329 if isinstance(population, list):
330 raise
331 return self.sample(tuple(population), k)
332 return result
333
334
335
336
337
341
342
343
345 """Normal distribution.
346
347 mu is the mean, and sigma is the standard deviation.
348
349 """
350
351
352
353
354
355
356
357 random = self.random
358 while 1:
359 u1 = random()
360 u2 = 1.0 - random()
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
366
367
368
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.
375
376 """
377 return _exp(self.normalvariate(mu, sigma))
378
379
380
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.
387
388 """
389
390
391
392 random = self.random
393 u = random()
394 while u <= 1e-7:
395 u = random()
396 return -_log(u)/lambd
397
398
399
401 """Circular data distribution.
402
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.
407
408 """
409
410
411
412
413
414
415
416
417
418
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)
427
428 while 1:
429 u1 = random()
430
431 z = _cos(_pi * u1)
432 f = (1.0 + r * z)/(r + z)
433 c = kappa * (r - f)
434
435 u2 = random()
436
437 if u2 < c * (2.0 - c) or u2 <= c * _exp(1.0 - c):
438 break
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
448
449
451 """Gamma distribution. Not the gamma function!
452
453 Conditions on the parameters are alpha > 0 and beta > 0.
454
455 """
456
457
458
459
460
461 if alpha <= 0.0 or beta <= 0.0:
462 raise ValueError, 'gammavariate: alpha and beta must be > 0.0'
463
464 random = self.random
465 if alpha > 1.0:
466
467
468
469
470
471 ainv = _sqrt(2.0 * alpha - 1.0)
472 bbb = alpha - LOG4
473 ccc = alpha + ainv
474
475 while 1:
476 u1 = random()
477 if not 1e-7 < u1 < .9999999:
478 continue
479 u2 = 1.0 - random()
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):
485 return x * beta
486
487 elif alpha == 1.0:
488
489 u = random()
490 while u <= 1e-7:
491 u = random()
492 return -_log(u) * beta
493
494 else:
495
496
497
498 while 1:
499 u = random()
500 b = (_e + alpha)/_e
501 p = b*u
502 if p <= 1.0:
503 x = p ** (1.0/alpha)
504 else:
505 x = -_log((b-p)/alpha)
506 u1 = random()
507 if p > 1.0:
508 if u1 <= x ** (alpha - 1.0):
509 break
510 elif u1 <= _exp(-x):
511 break
512 return x * beta
513
514
515
516 - def gauss(self, mu, sigma):
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.
523
524 """
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
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
552
553 return mu + z*sigma
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
570 """Beta distribution.
571
572 Conditions on the parameters are alpha > 0 and beta > 0.
573 Returned values range between 0 and 1.
574
575 """
576
577
578
579 y = self.gammavariate(alpha, 1.)
580 if y == 0:
581 return 0.0
582 else:
583 return y / (y + self.gammavariate(beta, 1.))
584
585
586
588 """Pareto distribution. alpha is the shape parameter."""
589
590
591 u = 1.0 - self.random()
592 return 1.0 / pow(u, 1.0/alpha)
593
594
595
597 """Weibull distribution.
598
599 alpha is the scale parameter and beta is the shape parameter.
600
601 """
602
603
604 u = 1.0 - self.random()
605 return alpha * pow(-_log(u), 1.0/beta)
606
607
608
610
611 VERSION = 1
612
614 """Initialize internal state from hashable object.
615
616 None or no argument seeds from current time or from an operating
617 system specific randomness source if available.
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:
628 try:
629 a = long(_hexlify(_urandom(16)), 16)
630 except NotImplementedError:
631 import time
632 a = long(time.time() * 256)
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
645 """Get the next random number in the range [0.0, 1.0)."""
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
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
670
671
672
673 return (x/30269.0 + y/30307.0 + z/30323.0) % 1.0
674
676 """Return internal state; can be passed to setstate() later."""
677 return self.VERSION, self._seed, self.gauss_next
678
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
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
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
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
730 self._seed = (x or 1, y or 1, z or 1)
731
732 self.gauss_next = None
733
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
757
758
760 """Alternate random number generator using sources provided
761 by the operating system (such as /dev/urandom on Unix or
762 CryptGenRandom on Windows).
763
764 Not available on all systems (see os.urandom() for details).
765 """
766
768 """Get the next random number in the range [0.0, 1.0)."""
769 return (long(_hexlify(_urandom(7)), 16) >> 3) * RECIP_BPF
770
772 """getrandbits(k) -> x. Generates a long int with k random bits."""
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
778 x = long(_hexlify(_urandom(bytes)), 16)
779 return x >> (bytes * 8 - k)
780
781 - def _stub(self, *args, **kwds):
782 "Stub method. Not used for a system random number generator."
783 return None
784 seed = jumpahead = _stub
785
787 "Method should not be called for a system random number generator."
788 raise NotImplementedError('System entropy source does not have state.')
789 getstate = setstate = _notimplemented
790
791
792
794 import time
795 print n, 'times', func.__name__
796 total = 0.0
797 sqsum = 0.0
798 smallest = 1e10
799 largest = -1e10
800 t0 = time.time()
801 for i in range(n):
802 x = func(*args)
803 total += x
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,',
809 avg = total/n
810 stddev = _sqrt(sqsum/n - avg*avg)
811 print 'avg %g, stddev %g, min %g, max %g' % \
812 (avg, stddev, smallest, largest)
813
814
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))
831
832
833
834
835
836
837
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
845 sample = _inst.sample
846 shuffle = _inst.shuffle
847 normalvariate = _inst.normalvariate
848 lognormvariate = _inst.lognormvariate
849 expovariate = _inst.expovariate
850 vonmisesvariate = _inst.vonmisesvariate
851 gammavariate = _inst.gammavariate
852 gauss = _inst.gauss
853 betavariate = _inst.betavariate
854 paretovariate = _inst.paretovariate
855 weibullvariate = _inst.weibullvariate
856 getstate = _inst.getstate
857 setstate = _inst.setstate
858 jumpahead = _inst.jumpahead
859 getrandbits = _inst.getrandbits
860
861 if __name__ == '__main__':
862 _test()
863