]>
git.proxmox.com Git - mirror_edk2.git/blob - AppPkg/Applications/Python/Python-2.7.10/Lib/random.py
1 """Random variable generators.
11 generate random permutation
13 distributions on the real line:
14 ------------------------------
25 distributions on the circle (angles 0 to 2pi)
26 ---------------------------------------------
30 General notes on the underlying Mersenne Twister core generator:
32 * The period is 2**19937-1.
33 * It is one of the most extensively tested generators in existence.
34 * Without a direct way to compute N steps forward, the semantics of
35 jumpahead(n) are weakened to simply jump to another distant state and rely
36 on the large period to avoid overlapping sequences.
37 * The random() method is implemented in C, executes in a single Python step,
38 and is, therefore, threadsafe.
42 from __future__
import division
43 from warnings
import warn
as _warn
44 from types
import MethodType
as _MethodType
, BuiltinMethodType
as _BuiltinMethodType
45 from math
import log
as _log
, exp
as _exp
, pi
as _pi
, e
as _e
, ceil
as _ceil
46 from math
import sqrt
as _sqrt
, acos
as _acos
, cos
as _cos
, sin
as _sin
47 from os
import urandom
as _urandom
48 from binascii
import hexlify
as _hexlify
49 import hashlib
as _hashlib
51 __all__
= ["Random","seed","random","uniform","randint","choice","sample",
52 "randrange","shuffle","normalvariate","lognormvariate",
53 "expovariate","vonmisesvariate","gammavariate","triangular",
54 "gauss","betavariate","paretovariate","weibullvariate",
55 "getstate","setstate","jumpahead", "WichmannHill", "getrandbits",
58 NV_MAGICCONST
= 4 * _exp(-0.5)/_sqrt(2.0)
61 SG_MAGICCONST
= 1.0 + _log(4.5)
62 BPF
= 53 # Number of bits in a float
66 # Translated by Guido van Rossum from C source provided by
67 # Adrian Baddeley. Adapted by Raymond Hettinger for use with
68 # the Mersenne Twister and os.urandom() core generators.
72 class Random(_random
.Random
):
73 """Random number generator base class used by bound module functions.
75 Used to instantiate instances of Random to get generators that don't
76 share state. Especially useful for multi-threaded programs, creating
77 a different instance of Random for each thread, and using the jumpahead()
78 method to ensure that the generated sequences seen by each thread don't
81 Class Random can also be subclassed if you want to use a different basic
82 generator of your own devising: in that case, override the following
83 methods: random(), seed(), getstate(), setstate() and jumpahead().
84 Optionally, implement a getrandbits() method so that randrange() can cover
85 arbitrarily large ranges.
89 VERSION
= 3 # used by getstate/setstate
91 def __init__(self
, x
=None):
92 """Initialize an instance.
94 Optional argument x controls seeding, as for Random.seed().
98 self
.gauss_next
= None
100 def seed(self
, a
=None):
101 """Initialize internal state from hashable object.
103 None or no argument seeds from current time or from an operating
104 system specific randomness source if available.
106 If a is not None or an int or long, hash(a) is used instead.
111 # Seed with enough bytes to span the 19937 bit
112 # state space for the Mersenne Twister
113 a
= long(_hexlify(_urandom(2500)), 16)
114 except NotImplementedError:
116 a
= long(time
.time() * 256) # use fractional seconds
118 super(Random
, self
).seed(a
)
119 self
.gauss_next
= None
122 """Return internal state; can be passed to setstate() later."""
123 return self
.VERSION
, super(Random
, self
).getstate(), self
.gauss_next
125 def setstate(self
, state
):
126 """Restore internal state from object returned by getstate()."""
129 version
, internalstate
, self
.gauss_next
= state
130 super(Random
, self
).setstate(internalstate
)
132 version
, internalstate
, self
.gauss_next
= state
133 # In version 2, the state was saved as signed ints, which causes
134 # inconsistencies between 32/64-bit systems. The state is
135 # really unsigned 32-bit ints, so we convert negative ints from
136 # version 2 to positive longs for version 3.
138 internalstate
= tuple( long(x
) % (2**32) for x
in internalstate
)
139 except ValueError, e
:
141 super(Random
, self
).setstate(internalstate
)
143 raise ValueError("state with version %s passed to "
144 "Random.setstate() of version %s" %
145 (version
, self
.VERSION
))
147 def jumpahead(self
, n
):
148 """Change the internal state to one that is likely far away
149 from the current state. This method will not be in Py3.x,
150 so it is better to simply reseed.
152 # The super.jumpahead() method uses shuffling to change state,
153 # so it needs a large and "interesting" n to work with. Here,
154 # we use hashing to create a large n for the shuffle.
155 s
= repr(n
) + repr(self
.getstate())
156 n
= int(_hashlib
.new('sha512', s
).hexdigest(), 16)
157 super(Random
, self
).jumpahead(n
)
159 ## ---- Methods below this point do not need to be overridden when
160 ## ---- subclassing for the purpose of using a different core generator.
162 ## -------------------- pickle support -------------------
164 def __getstate__(self
): # for pickle
165 return self
.getstate()
167 def __setstate__(self
, state
): # for pickle
170 def __reduce__(self
):
171 return self
.__class
__, (), self
.getstate()
173 ## -------------------- integer methods -------------------
175 def randrange(self
, start
, stop
=None, step
=1, _int
=int, _maxwidth
=1L<<BPF
):
176 """Choose a random item from range(start, stop[, step]).
178 This fixes the problem with randint() which includes the
179 endpoint; in Python this is usually not what you want.
183 # This code is a bit messy to make it fast for the
184 # common case while still doing adequate error checking.
187 raise ValueError, "non-integer arg 1 for randrange()"
190 if istart
>= _maxwidth
:
191 return self
._randbelow
(istart
)
192 return _int(self
.random() * istart
)
193 raise ValueError, "empty range for randrange()"
195 # stop argument supplied.
198 raise ValueError, "non-integer stop for randrange()"
199 width
= istop
- istart
200 if step
== 1 and width
> 0:
202 # int(istart + self.random()*width)
203 # instead would be incorrect. For example, consider istart
204 # = -2 and istop = 0. Then the guts would be in
205 # -2.0 to 0.0 exclusive on both ends (ignoring that random()
206 # might return 0.0), and because int() truncates toward 0, the
207 # final result would be -1 or 0 (instead of -2 or -1).
208 # istart + int(self.random()*width)
209 # would also be incorrect, for a subtler reason: the RHS
210 # can return a long, and then randrange() would also return
211 # a long, but we're supposed to return an int (for backward
214 if width
>= _maxwidth
:
215 return _int(istart
+ self
._randbelow
(width
))
216 return _int(istart
+ _int(self
.random()*width
))
218 raise ValueError, "empty range for randrange() (%d,%d, %d)" % (istart
, istop
, width
)
220 # Non-unit step argument supplied.
223 raise ValueError, "non-integer step for randrange()"
225 n
= (width
+ istep
- 1) // istep
227 n
= (width
+ istep
+ 1) // istep
229 raise ValueError, "zero step for randrange()"
232 raise ValueError, "empty range for randrange()"
235 return istart
+ istep
*self
._randbelow
(n
)
236 return istart
+ istep
*_int(self
.random() * n
)
238 def randint(self
, a
, b
):
239 """Return random integer in range [a, b], including both end points.
242 return self
.randrange(a
, b
+1)
244 def _randbelow(self
, n
, _log
=_log
, _int
=int, _maxwidth
=1L<<BPF
,
245 _Method
=_MethodType
, _BuiltinMethod
=_BuiltinMethodType
):
246 """Return a random int in the range [0,n)
248 Handles the case where n has more bits than returned
249 by a single call to the underlying generator.
253 getrandbits
= self
.getrandbits
254 except AttributeError:
257 # Only call self.getrandbits if the original random() builtin method
258 # has not been overridden or if a new getrandbits() was supplied.
259 # This assures that the two methods correspond.
260 if type(self
.random
) is _BuiltinMethod
or type(getrandbits
) is _Method
:
261 k
= _int(1.00001 + _log(n
-1, 2.0)) # 2**k > n-1 > 2**(k-2)
267 _warn("Underlying random() generator does not supply \n"
268 "enough bits to choose from a population range this large")
269 return _int(self
.random() * n
)
271 ## -------------------- sequence methods -------------------
273 def choice(self
, seq
):
274 """Choose a random element from a non-empty sequence."""
275 return seq
[int(self
.random() * len(seq
))] # raises IndexError if seq is empty
277 def shuffle(self
, x
, random
=None):
278 """x, random=random.random -> shuffle list x in place; return None.
280 Optional arg random is a 0-argument function returning a random
281 float in [0.0, 1.0); by default, the standard random.random.
288 for i
in reversed(xrange(1, len(x
))):
289 # pick an element in x[:i+1] with which to exchange x[i]
290 j
= _int(random() * (i
+1))
291 x
[i
], x
[j
] = x
[j
], x
[i
]
293 def sample(self
, population
, k
):
294 """Chooses k unique random elements from a population sequence.
296 Returns a new list containing elements from the population while
297 leaving the original population unchanged. The resulting list is
298 in selection order so that all sub-slices will also be valid random
299 samples. This allows raffle winners (the sample) to be partitioned
300 into grand prize and second place winners (the subslices).
302 Members of the population need not be hashable or unique. If the
303 population contains repeats, then each occurrence is a possible
304 selection in the sample.
306 To choose a sample in a range of integers, use xrange as an argument.
307 This is especially fast and space efficient for sampling from a
308 large population: sample(xrange(10000000), 60)
311 # Sampling without replacement entails tracking either potential
312 # selections (the pool) in a list or previous selections in a set.
314 # When the number of selections is small compared to the
315 # population, then tracking selections is efficient, requiring
316 # only a small set and an occasional reselection. For
317 # a larger number of selections, the pool tracking method is
318 # preferred since the list takes less space than the
319 # set and it doesn't suffer from frequent reselections.
323 raise ValueError("sample larger than population")
327 setsize
= 21 # size of a small set minus size of an empty list
329 setsize
+= 4 ** _ceil(_log(k
* 3, 4)) # table size for big sets
330 if n
<= setsize
or hasattr(population
, "keys"):
331 # An n-length list is smaller than a k-length set, or this is a
332 # mapping type so the other algorithm wouldn't work.
333 pool
= list(population
)
334 for i
in xrange(k
): # invariant: non-selected at [0,n-i)
335 j
= _int(random() * (n
-i
))
337 pool
[j
] = pool
[n
-i
-1] # move non-selected item into vacancy
341 selected_add
= selected
.add
343 j
= _int(random() * n
)
345 j
= _int(random() * n
)
347 result
[i
] = population
[j
]
348 except (TypeError, KeyError): # handle (at least) sets
349 if isinstance(population
, list):
351 return self
.sample(tuple(population
), k
)
354 ## -------------------- real-valued distributions -------------------
356 ## -------------------- uniform distribution -------------------
358 def uniform(self
, a
, b
):
359 "Get a random number in the range [a, b) or [a, b] depending on rounding."
360 return a
+ (b
-a
) * self
.random()
362 ## -------------------- triangular --------------------
364 def triangular(self
, low
=0.0, high
=1.0, mode
=None):
365 """Triangular distribution.
367 Continuous distribution bounded by given lower and upper limits,
368 and having a given mode value in-between.
370 http://en.wikipedia.org/wiki/Triangular_distribution
375 c
= 0.5 if mode
is None else (mode
- low
) / (high
- low
)
376 except ZeroDivisionError:
381 low
, high
= high
, low
382 return low
+ (high
- low
) * (u
* c
) ** 0.5
384 ## -------------------- normal distribution --------------------
386 def normalvariate(self
, mu
, sigma
):
387 """Normal distribution.
389 mu is the mean, and sigma is the standard deviation.
392 # mu = mean, sigma = standard deviation
394 # Uses Kinderman and Monahan method. Reference: Kinderman,
395 # A.J. and Monahan, J.F., "Computer generation of random
396 # variables using the ratio of uniform deviates", ACM Trans
397 # Math Software, 3, (1977), pp257-260.
403 z
= NV_MAGICCONST
*(u1
-0.5)/u2
409 ## -------------------- lognormal distribution --------------------
411 def lognormvariate(self
, mu
, sigma
):
412 """Log normal distribution.
414 If you take the natural logarithm of this distribution, you'll get a
415 normal distribution with mean mu and standard deviation sigma.
416 mu can have any value, and sigma must be greater than zero.
419 return _exp(self
.normalvariate(mu
, sigma
))
421 ## -------------------- exponential distribution --------------------
423 def expovariate(self
, lambd
):
424 """Exponential distribution.
426 lambd is 1.0 divided by the desired mean. It should be
427 nonzero. (The parameter would be called "lambda", but that is
428 a reserved word in Python.) Returned values range from 0 to
429 positive infinity if lambd is positive, and from negative
430 infinity to 0 if lambd is negative.
433 # lambd: rate lambd = 1/mean
434 # ('lambda' is a Python reserved word)
436 # we use 1-random() instead of random() to preclude the
437 # possibility of taking the log of zero.
438 return -_log(1.0 - self
.random())/lambd
440 ## -------------------- von Mises distribution --------------------
442 def vonmisesvariate(self
, mu
, kappa
):
443 """Circular data distribution.
445 mu is the mean angle, expressed in radians between 0 and 2*pi, and
446 kappa is the concentration parameter, which must be greater than or
447 equal to zero. If kappa is equal to zero, this distribution reduces
448 to a uniform random angle over the range 0 to 2*pi.
451 # mu: mean angle (in radians between 0 and 2*pi)
452 # kappa: concentration parameter kappa (>= 0)
453 # if kappa = 0 generate uniform random angle
455 # Based upon an algorithm published in: Fisher, N.I.,
456 # "Statistical Analysis of Circular Data", Cambridge
457 # University Press, 1993.
459 # Thanks to Magnus Kessler for a correction to the
460 # implementation of step 4.
464 return TWOPI
* random()
467 r
= s
+ _sqrt(1.0 + s
* s
)
475 if u2
< 1.0 - d
* d
or u2
<= (1.0 - d
) * _exp(d
):
479 f
= (q
+ z
) / (1.0 + q
* z
)
482 theta
= (mu
+ _acos(f
)) % TWOPI
484 theta
= (mu
- _acos(f
)) % TWOPI
488 ## -------------------- gamma distribution --------------------
490 def gammavariate(self
, alpha
, beta
):
491 """Gamma distribution. Not the gamma function!
493 Conditions on the parameters are alpha > 0 and beta > 0.
495 The probability distribution function is:
497 x ** (alpha - 1) * math.exp(-x / beta)
498 pdf(x) = --------------------------------------
499 math.gamma(alpha) * beta ** alpha
503 # alpha > 0, beta > 0, mean is alpha*beta, variance is alpha*beta**2
505 # Warning: a few older sources define the gamma distribution in terms
507 if alpha
<= 0.0 or beta
<= 0.0:
508 raise ValueError, 'gammavariate: alpha and beta must be > 0.0'
513 # Uses R.C.H. Cheng, "The generation of Gamma
514 # variables with non-integral shape parameters",
515 # Applied Statistics, (1977), 26, No. 1, p71-74
517 ainv
= _sqrt(2.0 * alpha
- 1.0)
523 if not 1e-7 < u1
< .9999999:
526 v
= _log(u1
/(1.0-u1
))/ainv
530 if r
+ SG_MAGICCONST
- 4.5*z
>= 0.0 or r
>= _log(z
):
538 return -_log(u
) * beta
540 else: # alpha is between 0 and 1 (exclusive)
542 # Uses ALGORITHM GS of Statistical Computing - Kennedy & Gentle
551 x
= -_log((b
-p
)/alpha
)
554 if u1
<= x
** (alpha
- 1.0):
560 ## -------------------- Gauss (faster alternative) --------------------
562 def gauss(self
, mu
, sigma
):
563 """Gaussian distribution.
565 mu is the mean, and sigma is the standard deviation. This is
566 slightly faster than the normalvariate() function.
568 Not thread-safe without a lock around calls.
572 # When x and y are two variables from [0, 1), uniformly
575 # cos(2*pi*x)*sqrt(-2*log(1-y))
576 # sin(2*pi*x)*sqrt(-2*log(1-y))
578 # are two *independent* variables with normal distribution
579 # (mu = 0, sigma = 1).
581 # (corrected version; bug discovered by Mike Miller, fixed by LM)
583 # Multithreading note: When two threads call this function
584 # simultaneously, it is possible that they will receive the
585 # same return value. The window is very small though. To
586 # avoid this, you have to use a lock around all calls. (I
587 # didn't want to slow this down in the serial case by using a
592 self
.gauss_next
= None
594 x2pi
= random() * TWOPI
595 g2rad
= _sqrt(-2.0 * _log(1.0 - random()))
596 z
= _cos(x2pi
) * g2rad
597 self
.gauss_next
= _sin(x2pi
) * g2rad
601 ## -------------------- beta --------------------
603 ## http://mail.python.org/pipermail/python-bugs-list/2001-January/003752.html
604 ## for Ivan Frohne's insightful analysis of why the original implementation:
606 ## def betavariate(self, alpha, beta):
607 ## # Discrete Event Simulation in C, pp 87-88.
609 ## y = self.expovariate(alpha)
610 ## z = self.expovariate(1.0/beta)
613 ## was dead wrong, and how it probably got that way.
615 def betavariate(self
, alpha
, beta
):
616 """Beta distribution.
618 Conditions on the parameters are alpha > 0 and beta > 0.
619 Returned values range between 0 and 1.
623 # This version due to Janne Sinkkonen, and matches all the std
624 # texts (e.g., Knuth Vol 2 Ed 3 pg 134 "the beta distribution").
625 y
= self
.gammavariate(alpha
, 1.)
629 return y
/ (y
+ self
.gammavariate(beta
, 1.))
631 ## -------------------- Pareto --------------------
633 def paretovariate(self
, alpha
):
634 """Pareto distribution. alpha is the shape parameter."""
637 u
= 1.0 - self
.random()
638 return 1.0 / pow(u
, 1.0/alpha
)
640 ## -------------------- Weibull --------------------
642 def weibullvariate(self
, alpha
, beta
):
643 """Weibull distribution.
645 alpha is the scale parameter and beta is the shape parameter.
648 # Jain, pg. 499; bug fix courtesy Bill Arms
650 u
= 1.0 - self
.random()
651 return alpha
* pow(-_log(u
), 1.0/beta
)
653 ## -------------------- Wichmann-Hill -------------------
655 class WichmannHill(Random
):
657 VERSION
= 1 # used by getstate/setstate
659 def seed(self
, a
=None):
660 """Initialize internal state from hashable object.
662 None or no argument seeds from current time or from an operating
663 system specific randomness source if available.
665 If a is not None or an int or long, hash(a) is used instead.
667 If a is an int or long, a is used directly. Distinct values between
668 0 and 27814431486575L inclusive are guaranteed to yield distinct
669 internal states (this guarantee is specific to the default
670 Wichmann-Hill generator).
675 a
= long(_hexlify(_urandom(16)), 16)
676 except NotImplementedError:
678 a
= long(time
.time() * 256) # use fractional seconds
680 if not isinstance(a
, (int, long)):
683 a
, x
= divmod(a
, 30268)
684 a
, y
= divmod(a
, 30306)
685 a
, z
= divmod(a
, 30322)
686 self
._seed
= int(x
)+1, int(y
)+1, int(z
)+1
688 self
.gauss_next
= None
691 """Get the next random number in the range [0.0, 1.0)."""
693 # Wichman-Hill random number generator.
695 # Wichmann, B. A. & Hill, I. D. (1982)
697 # An efficient and portable pseudo-random number generator
698 # Applied Statistics 31 (1982) 188-190
701 # Correction to Algorithm AS 183
702 # Applied Statistics 33 (1984) 123
704 # McLeod, A. I. (1985)
705 # A remark on Algorithm AS 183
706 # Applied Statistics 34 (1985),198-200
708 # This part is thread-unsafe:
709 # BEGIN CRITICAL SECTION
711 x
= (171 * x
) % 30269
712 y
= (172 * y
) % 30307
713 z
= (170 * z
) % 30323
715 # END CRITICAL SECTION
717 # Note: on a platform using IEEE-754 double arithmetic, this can
718 # never return 0.0 (asserted by Tim; proof too long for a comment).
719 return (x
/30269.0 + y
/30307.0 + z
/30323.0) % 1.0
722 """Return internal state; can be passed to setstate() later."""
723 return self
.VERSION
, self
._seed
, self
.gauss_next
725 def setstate(self
, state
):
726 """Restore internal state from object returned by getstate()."""
729 version
, self
._seed
, self
.gauss_next
= state
731 raise ValueError("state with version %s passed to "
732 "Random.setstate() of version %s" %
733 (version
, self
.VERSION
))
735 def jumpahead(self
, n
):
736 """Act as if n calls to random() were made, but quickly.
738 n is an int, greater than or equal to 0.
740 Example use: If you have 2 threads and know that each will
741 consume no more than a million random numbers, create two Random
742 objects r1 and r2, then do
743 r2.setstate(r1.getstate())
744 r2.jumpahead(1000000)
745 Then r1 and r2 will use guaranteed-disjoint segments of the full
750 raise ValueError("n must be >= 0")
752 x
= int(x
* pow(171, n
, 30269)) % 30269
753 y
= int(y
* pow(172, n
, 30307)) % 30307
754 z
= int(z
* pow(170, n
, 30323)) % 30323
757 def __whseed(self
, x
=0, y
=0, z
=0):
758 """Set the Wichmann-Hill seed from (x, y, z).
760 These must be integers in the range [0, 256).
763 if not type(x
) == type(y
) == type(z
) == int:
764 raise TypeError('seeds must be integers')
765 if not (0 <= x
< 256 and 0 <= y
< 256 and 0 <= z
< 256):
766 raise ValueError('seeds must be in range(0, 256)')
768 # Initialize from current time
770 t
= long(time
.time() * 256)
771 t
= int((t
&0xffffff) ^
(t
>>24))
772 t
, x
= divmod(t
, 256)
773 t
, y
= divmod(t
, 256)
774 t
, z
= divmod(t
, 256)
775 # Zero is a poor seed, so substitute 1
776 self
._seed
= (x
or 1, y
or 1, z
or 1)
778 self
.gauss_next
= None
780 def whseed(self
, a
=None):
781 """Seed from hashable object's hash code.
783 None or no argument seeds from current time. It is not guaranteed
784 that objects with distinct hash codes lead to distinct internal
787 This is obsolete, provided for compatibility with the seed routine
788 used prior to Python 2.1. Use the .seed() method instead.
795 a
, x
= divmod(a
, 256)
796 a
, y
= divmod(a
, 256)
797 a
, z
= divmod(a
, 256)
798 x
= (x
+ a
) % 256 or 1
799 y
= (y
+ a
) % 256 or 1
800 z
= (z
+ a
) % 256 or 1
801 self
.__whseed
(x
, y
, z
)
803 ## --------------- Operating System Random Source ------------------
805 class SystemRandom(Random
):
806 """Alternate random number generator using sources provided
807 by the operating system (such as /dev/urandom on Unix or
808 CryptGenRandom on Windows).
810 Not available on all systems (see os.urandom() for details).
814 """Get the next random number in the range [0.0, 1.0)."""
815 return (long(_hexlify(_urandom(7)), 16) >> 3) * RECIP_BPF
817 def getrandbits(self
, k
):
818 """getrandbits(k) -> x. Generates a long int with k random bits."""
820 raise ValueError('number of bits must be greater than zero')
822 raise TypeError('number of bits should be an integer')
823 bytes
= (k
+ 7) // 8 # bits / 8 and rounded up
824 x
= long(_hexlify(_urandom(bytes
)), 16)
825 return x
>> (bytes
* 8 - k
) # trim excess bits
827 def _stub(self
, *args
, **kwds
):
828 "Stub method. Not used for a system random number generator."
830 seed
= jumpahead
= _stub
832 def _notimplemented(self
, *args
, **kwds
):
833 "Method should not be called for a system random number generator."
834 raise NotImplementedError('System entropy source does not have state.')
835 getstate
= setstate
= _notimplemented
837 ## -------------------- test program --------------------
839 def _test_generator(n
, func
, args
):
841 print n
, 'times', func
.__name
__
851 smallest
= min(x
, smallest
)
852 largest
= max(x
, largest
)
854 print round(t1
-t0
, 3), 'sec,',
856 stddev
= _sqrt(sqsum
/n
- avg
*avg
)
857 print 'avg %g, stddev %g, min %g, max %g' % \
858 (avg
, stddev
, smallest
, largest
)
862 _test_generator(N
, random
, ())
863 _test_generator(N
, normalvariate
, (0.0, 1.0))
864 _test_generator(N
, lognormvariate
, (0.0, 1.0))
865 _test_generator(N
, vonmisesvariate
, (0.0, 1.0))
866 _test_generator(N
, gammavariate
, (0.01, 1.0))
867 _test_generator(N
, gammavariate
, (0.1, 1.0))
868 _test_generator(N
, gammavariate
, (0.1, 2.0))
869 _test_generator(N
, gammavariate
, (0.5, 1.0))
870 _test_generator(N
, gammavariate
, (0.9, 1.0))
871 _test_generator(N
, gammavariate
, (1.0, 1.0))
872 _test_generator(N
, gammavariate
, (2.0, 1.0))
873 _test_generator(N
, gammavariate
, (20.0, 1.0))
874 _test_generator(N
, gammavariate
, (200.0, 1.0))
875 _test_generator(N
, gauss
, (0.0, 1.0))
876 _test_generator(N
, betavariate
, (3.0, 3.0))
877 _test_generator(N
, triangular
, (0.0, 1.0, 1.0/3.0))
879 # Create one instance, seeded from current time, and export its methods
880 # as module-level functions. The functions share state across all uses
881 #(both in the user's code and in the Python libraries), but that's fine
882 # for most programs and is easier for the casual user than making them
883 # instantiate their own Random() instance.
887 random
= _inst
.random
888 uniform
= _inst
.uniform
889 triangular
= _inst
.triangular
890 randint
= _inst
.randint
891 choice
= _inst
.choice
892 randrange
= _inst
.randrange
893 sample
= _inst
.sample
894 shuffle
= _inst
.shuffle
895 normalvariate
= _inst
.normalvariate
896 lognormvariate
= _inst
.lognormvariate
897 expovariate
= _inst
.expovariate
898 vonmisesvariate
= _inst
.vonmisesvariate
899 gammavariate
= _inst
.gammavariate
901 betavariate
= _inst
.betavariate
902 paretovariate
= _inst
.paretovariate
903 weibullvariate
= _inst
.weibullvariate
904 getstate
= _inst
.getstate
905 setstate
= _inst
.setstate
906 jumpahead
= _inst
.jumpahead
907 getrandbits
= _inst
.getrandbits
909 if __name__
== '__main__':