random :: Random :: Class Random
[hide private]
[frames] | no frames]

Class Random

source code

    object --+    
             |    
_random.Random --+
                 |
                Random
Known Subclasses:
SystemRandom, WichmannHill

Random number generator base class used by bound module functions.

Used to instantiate instances of Random to get generators that don't share state. Especially useful for multi-threaded programs, creating a different instance of Random for each thread, and using the jumpahead() method to ensure that the generated sequences seen by each thread don't overlap.

Class Random can also be subclassed if you want to use a different basic generator of your own devising: in that case, override the following methods: random(), seed(), getstate(), setstate() and jumpahead(). Optionally, implement a getrandombits() method so that randrange() can cover arbitrarily large ranges.

Instance Methods [hide private]
 
__getstate__(self) source code
 
__init__(self, x=None)
Initialize an instance.
source code
 
__reduce__(self)
helper for pickle
source code
 
__setstate__(self, state) source code
 
_randbelow(self, n, _log=<built-in function log>, int=<type 'int'>, _maxwidth=9007199254740992, _Method=<type 'instancemethod'>, _BuiltinMethod=<type 'builtin_function_or_method'>)
Return a random int in the range [0,n)
source code
 
betavariate(self, alpha, beta)
Beta distribution.
source code
 
choice(self, seq)
Choose a random element from a non-empty sequence.
source code
 
expovariate(self, lambd)
Exponential distribution.
source code
 
gammavariate(self, alpha, beta)
Gamma distribution.
source code
 
gauss(self, mu, sigma)
Gaussian distribution.
source code
 
getstate(self)
Return internal state; can be passed to setstate() later.
source code
 
lognormvariate(self, mu, sigma)
Log normal distribution.
source code
 
normalvariate(self, mu, sigma)
Normal distribution.
source code
 
paretovariate(self, alpha)
Pareto distribution.
source code
 
randint(self, a, b)
Return random integer in range [a, b], including both end points.
source code
 
randrange(self, start, stop=None, step=1, int=<type 'int'>, default=None, maxwidth=9007199254740992)
Choose a random item from range(start, stop[, step]).
source code
 
sample(self, population, k)
Chooses k unique random elements from a population sequence.
source code
 
seed(self, a=None)
Initialize internal state from hashable object.
source code
 
setstate(self, state)
Restore internal state from object returned by getstate().
source code
 
shuffle(self, x, random=None, int=<type 'int'>)
x, random=random.random -> shuffle list x in place; return None.
source code
 
uniform(self, a, b)
Get a random number in the range [a, b).
source code
 
vonmisesvariate(self, mu, kappa)
Circular data distribution.
source code
 
weibullvariate(self, alpha, beta)
Weibull distribution.
source code

Inherited from _random.Random: __getattribute__, __new__, getrandbits, jumpahead, random

Inherited from object: __delattr__, __hash__, __reduce_ex__, __repr__, __setattr__, __str__

Class Variables [hide private]
  VERSION = 2
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, x=None)
(Constructor)

source code 

Initialize an instance.

Optional argument x controls seeding, as for Random.seed().
Overrides: object.__init__

__reduce__(self)

source code 
helper for pickle
Overrides: object.__reduce__
(inherited documentation)

_randbelow(self, n, _log=<built-in function log>, int=<type 'int'>, _maxwidth=9007199254740992, _Method=<type 'instancemethod'>, _BuiltinMethod=<type 'builtin_function_or_method'>)

source code 

Return a random int in the range [0,n)

Handles the case where n has more bits than returned by a single call to the underlying generator.

betavariate(self, alpha, beta)

source code 
Beta distribution.

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

expovariate(self, 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(self, alpha, beta)

source code 

Gamma distribution. Not the gamma function!

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

gauss(self, 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.

getstate(self)

source code 
Return internal state; can be passed to setstate() later.
Returns:
tuple containing the current state.

Overrides: _random.Random.getstate

lognormvariate(self, 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(self, mu, sigma)

source code 

Normal distribution.

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

paretovariate(self, alpha)

source code 
Pareto distribution. alpha is the shape parameter.

randrange(self, 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(self, 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(self, 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.
Returns:
None

Overrides: _random.Random.seed

setstate(self, state)

source code 
Restore internal state from object returned by getstate().
Returns:
None

Overrides: _random.Random.setstate

shuffle(self, 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(self, 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(self, alpha, beta)

source code 

Weibull distribution.

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