This module implements pseudo-random number generators for various distributions.
-
seed( [x]) - Initialize the basic random number generator. Optional argument x can be any hashable object. If x is omitted or None, current system time is used; current system time is also used to initialize the generator when the module is first imported. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability). Changed in version 2.4: formerly, operating system resources were not used. If x is not None or an int or long, hash(x) is used instead. If x is an int or long, x is used directly.
-
getstate( ) - Return an object capturing the current internal state of the generator. This object can be passed to setstate() to restore the state. New in version 2.1.
getstate()
(2, (-2147483648, -1498780658, 190942503, -1397184856, 112039908,
228955763, 859386857, -1703970711, -1022417208, -1638064546, -1381775298,
893651760, -1735202666, 2039146853, 1277323948, 748566623, -1437745388,
-1007958509, -829204929, -124813626, 1844974135, 1299535405, 1370033025,
1174283154, -1786962080, -198238382, -48121607, 1939254678, -1732847710,
95192785, -1086460089, -1889916975, 228185975, 1438999711, -1250749932,
...
-
setstate( state) - state should have been obtained from a previous call to getstate(), and setstate() restores the internal state of the generator to what it was at the time setstate() was called. New in version 2.1.
-
jumpahead( n) - Change the internal state to one different from and likely far away from the current state. n is a non-negative integer which is used to scramble the current state vector. This is most useful in multi-threaded programs, in conjuction with multiple instances of the Random class: setstate() or seed() can be used to force all instances into the same internal state, and then jumpahead() can be used to force the instances' states far apart. New in version 2.1. Changed in version 2.3: Instead of jumping to a specific state, n steps ahead, jumpahead(n) jumps to another state likely to be separated by many steps.
-
getrandbits( k) - Returns a python long int with k random bits. This method is supplied with the MersenneTwister generator and some other generators may also provide it as an optional part of the API. When available, getrandbits() enables randrange() to handle arbitrarily large ranges. New in version 2.4.
getrandbits(5) # 19L
getrandbits(20) # 267853L
-
randrange( [start,] stop[, step]) - Return a randomly selected element from range(start, stop, step). This is equivalent to choice(range(start, stop, step)), but doesn't actually build a range object. New in version 1.5.2.
randrange(5) # 3
randrange(25) # 19
-
randint( a, b) - Return a random integer N such that a <= N <= b.
randint(3, 18) # 9
-
choice( seq) - Return a random element from the non-empty sequence seq. If seq is empty, raises IndexError.
choice("abcdef") # 'f'
choice([1,2,3,4,5]) # 4
-
shuffle( x[, random]) - Shuffle the sequence x in place. The optional argument random is a 0-argument function returning a random float in [0.0, 1.0); by default, this is the function random().
Note that for even rather small len(x), the total number of permutations of x is larger than the period of most random number generators; this implies that most permutations of a long sequence can never be generated.
x = list('abcdef')
shuffle(x)
x # ['c', 'd', 'e', 'b', 'a', 'f']
-
sample( population, k) - Return a k length list of unique elements chosen from the population sequence. Used for random sampling without replacement. New in version 2.3.
Returns a new list containing elements from the population while leaving the original population unchanged. The resulting list is in selection order so that all sub-slices will also be valid random samples. This allows raffle winners (the sample) to be partitioned into grand prize and second place winners (the subslices).
Members of the population need not be hashable or unique. If the population contains repeats, then each occurrence is a possible selection in the sample.
sample("abcdef", 3) # ['a', 'b', 'c']
-
random( ) - Return the next random floating point number in the range [0.0, 1.0). - can be 0.0 but never 1.0.
random() # 0.0033831193743567578
random() # 0.67793424954769121
random() # 0.33789686162786514
-
uniform( a, b) - Return a random real number N such that a <= N < b.
uniform(1,10) # 8.3666626718236365
uniform(1,10) # 5.3267066796703508
-
betavariate( alpha, beta) - Beta distribution. Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1.
betavariate(1, 10) # 0.10496249499203709
betavariate(1, 10) # 0.18214023619683944
betavariate(1, 10) # 0.2290416841244714
-
expovariate( lambd) - Exponential distribution. lambd is 1.0 divided by the desired mean. (The parameter would be called ``lambda'', but that is a reserved word in Python.) Returned values range from 0 to positive infinity.
expovariate(1.0/5) # 2.7364209240544182
expovariate(1.0/5) # 23.509293826002065
-
gammavariate( alpha, beta) - Gamma distribution. (Not the gamma function!) Conditions on the parameters are alpha > 0 and beta > 0.
gammavariate(2,10) # 3.5067453839753866
gammavariate(2,10) # 38.390786490427715
-
gauss( mu, sigma) - Gaussian distribution. mu is the mean, and sigma is the standard deviation. This is slightly faster than the normalvariate() function defined below.
gauss(5,20) # 22.172658189034109
gauss(5,20) # -1.5453161867542757
-
lognormvariate( mu, sigma) - Log normal distribution. If you take the natural logarithm of this distribution, you'll get a normal distribution with mean mu and standard deviation sigma. mu can have any value, and sigma must be greater than zero.
lognormvariate(5,12) # 0.27547360714592867
lognormvariate(5,12) # 85903.28553513001
-
normalvariate( mu, sigma) - Normal distribution. mu is the mean, and sigma is the standard deviation.
normalvariate(5,12) # 30.218332993634455
normalvariate(5,12) # -3.4244614624457981
-
vonmisesvariate( mu, kappa) - mu is the mean angle, expressed in radians between 0 and 2*pi, and kappa is the concentration parameter, which must be greater than or equal to zero. If kappa is equal to zero, this distribution reduces to a uniform random angle over the range 0 to 2*pi.
vonmisesvariate(3,1) # 5.6210863430495461
-
paretovariate( alpha) - Pareto distribution. alpha is the shape parameter.
paretovariate(3) # 1.3002954290088935
-
weibullvariate( alpha, beta) - Weibull distribution. alpha is the scale parameter and beta is the shape parameter.
weibullvariate(3,1) # 1.1238637860009177
index
