Module random
[hide private]
[frames] | no frames]

Module random

source code

Random variable generators.

    integers
    --------
           uniform within range

    sequences
    ---------
           pick random element
           pick random sample
           generate random permutation

    distributions on the real line:
    ------------------------------
           uniform
           normal (Gaussian)
           lognormal
           negative exponential
           gamma
           beta
           pareto
           Weibull

    distributions on the circle (angles 0 to 2pi)
    ---------------------------------------------
           circular uniform
           von Mises

General notes on the underlying Mersenne Twister core generator:

* The period is 2**19937-1.
* It is one of the most extensively tested generators in existence.
* Without a direct way to compute N steps forward, the semantics of
  jumpahead(n) are weakened to simply jump to another distant state and rely
  on the large period to avoid overlapping sequences.
* The random() method is implemented in C, executes in a single Python step,
  and is, therefore, threadsafe.



Classes [hide private]
  Random
Random number generator base class used by bound module functions.
  SystemRandom
Alternate random number generator using sources provided by the operating system (such as /dev/urandom on Unix or CryptGenRandom on Windows).
  WichmannHill
Functions [hide private]
 
_test(N=2000) source code
 
_test_generator(n, func, args) source code
 
betavariate(alpha, beta)
Beta distribution.
source code
 
choice(seq)
Choose a random element from a non-empty sequence.
source code
 
expovariate(lambd)
Exponential distribution.
source code
 
gammavariate(alpha, beta)
Gamma distribution.
source code
 
gauss(mu, sigma)
Gaussian distribution.
source code
 
getrandbits(k)
Generates a long int with k random bits.
source code
 
getstate()
Return internal state; can be passed to setstate() later.
source code
 
jumpahead(int)
Create new state from existing state and integer.
source code
 
lognormvariate(mu, sigma)
Log normal distribution.
source code
 
normalvariate(mu, sigma)
Normal distribution.
source code
 
paretovariate(alpha)
Pareto distribution.
source code
 
randint(a, b)
Return random integer in range [a, b], including both end points.
source code
 
random()
Returns: x in the interval [0, 1).
source code
 
randrange(start, stop=None, step=1, int=<type 'int'>, default=None, maxwidth=9007199254740992)
Choose a random item from range(start, stop[, step]).
source code
 
sample(population, k)
Chooses k unique random elements from a population sequence.
source code
 
seed(a=None)
Initialize internal state from hashable object.
source code
 
setstate(state)
Restore internal state from object returned by getstate().
source code
 
shuffle(x, random=None, int=<type 'int'>)
x, random=random.random -> shuffle list x in place; return None.
source code
 
uniform(a, b)
Get a random number in the range [a, b).
source code
 
vonmisesvariate(mu, kappa)
Circular data distribution.
source code
 
weibullvariate(alpha, beta)
Weibull distribution.
source code
Variables [hide private]
  BPF = 53
  LOG4 = 1.38629436112
  NV_MAGICCONST = 1.71552776992
  RECIP_BPF = 1.11022302463e-16
  SG_MAGICCONST = 2.50407739678
  TWOPI = 6.28318530718
  __allow_access_to_unprotected_subobjects__ = 1
  _e = 2.71828182846
  _inst = <random.Random object at 0x1868610>
  _pi = 3.14159265359
Function Details [hide private]

betavariate(alpha, beta)

source code 
Beta distribution.

Conditions on the parameters are alpha > -1 and beta} > -1.
Returned values range between 0 and 1.

expovariate(lambd)

source code 

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.

gammavariate(alpha, beta)

source code 

Gamma distribution. Not the gamma function!

Conditions on the parameters are alpha > 0 and beta > 0.

gauss(mu, sigma)

source code 

Gaussian distribution.

mu is the mean, and sigma is the standard deviation. This is slightly faster than the normalvariate() function.

Not thread-safe without a lock around calls.

getrandbits(k)

source code 
Generates a long int with k random bits.
Returns:
x

jumpahead(int)

source code 
Create new state from existing state and integer.
Returns:
None

lognormvariate(mu, sigma)

source code 

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.

normalvariate(mu, sigma)

source code 

Normal distribution.

mu is the mean, and sigma is the standard deviation.

paretovariate(alpha)

source code 
Pareto distribution. alpha is the shape parameter.

random()

source code 
Returns:
x in the interval [0, 1).

randrange(start, stop=None, step=1, int=<type 'int'>, default=None, maxwidth=9007199254740992)

source code 

Choose a random item from range(start, stop[, step]).

This fixes the problem with randint() which includes the endpoint; in Python this is usually not what you want. Do not supply the 'int', 'default', and 'maxwidth' arguments.

sample(population, k)

source code 

Chooses k unique random elements from a population sequence.

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.

To choose a sample in a range of integers, use xrange as an argument. This is especially fast and space efficient for sampling from a large population: sample(xrange(10000000), 60)

seed(a=None)

source code 

Initialize internal state from hashable object.

None or no argument seeds from current time or from an operating system specific randomness source if available.

If a is not None or an int or long, hash(a) is used instead.

shuffle(x, random=None, int=<type 'int'>)

source code 

x, random=random.random -> shuffle list x in place; return None.

Optional arg random is a 0-argument function returning a random float in [0.0, 1.0); by default, the standard random.random.

vonmisesvariate(mu, kappa)

source code 

Circular data distribution.

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.

weibullvariate(alpha, beta)

source code 

Weibull distribution.

alpha is the scale parameter and beta is the shape parameter.