import random
random()
is the most basic function of the random module.random()
return the next random floating-point number in the range [0.0, 1.0).random
module has a set of methods which are defined below :-random.
seed
()Initialize the random number generator.
random.
getstate
()Return an object capturing the current internal state of the generator. This object can be passed to setstate()
to restore the state.
random.
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 getstate()
was called.
random.
getrandbits
(k)Returns a Python integer with k random bits.
Functions for Integers :
random.
randrange
(stop)random.
randrange
(start, stop, step)random.
randint
(a, b)Return a random integer N such that a <= N <= b
.
random.
choice
(seq)Return a random element from the non-empty sequence seq. seq is empty will raises to index Error.
random.
choices
(seq,k=1)Return a k sized list of elements chosen from the seq. If the seq is empty, raises index Error.
random.
shuffle
(x[, random])Take a sequence and returns the sequence in random order.
random.
sample
(seq, k)Return a k length list of unique elements chosen from the seq .
The following functions generate specific real-valued distributions. Function parameters are named after the corresponding variables in the distributions equation, as used in common mathematical practice; most of these equations can be found in any statistics text.
random.
random
()Return the next random floating point number in the range [0.0, 1.0).
random.
uniform
(a, b)Return a random floating point number N such that a <= N <= b
for a <= b
and b <= N <= a
for b < a
.
random.
triangular
(low, high, mode)Return a random floating point number N such that low <= N <= high
and with the specified mode between those bounds. The low and high bounds default to zero and one. The mode argument defaults to the midpoint between the bounds, giving a symmetric distribution.
random.
betavariate
(alpha, beta)Returns a random float number between 0 and 1.Conditions on the parameters are alpha > 0
and beta > 0
random.
expovariate
(lmbd)Exponential distribution. lmbd is 1.0 divided by the desired mean. It should be nonzero. (The parameter would be called lambda, but that is a reserved word in Python.) Returned values range from 0 to positive infinity if lmbd is positive, and from negative infinity to 0 if lmbd is negative.
random.
gammavariate
(alpha, beta)Gamma distribution. (Not the gamma function!) Conditions on the parameters are alpha > 0
and beta > 0
.
random.
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.
random.
lognormvariate
(mu, sigma)Log normal distribution. If you take the natural logarithm of this distribution, you get a normal distribution with mean mu and standard deviation sigma. mu can have any value, and sigma must be greater than zero.
random.
normalvariate
(mu, sigma)Normal distribution. mu is the mean, and sigma is the standard deviation.
random.
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.
random.
paretovariate
(alpha)Pareto distribution. alpha is the shape parameter.
random.
weibullvariate
(alpha, beta)Weibull distribution. alpha is the scale parameter and beta is the shape parameter.
random.
Random
([seed])Class that implements the default pseudo-random number generator used by the random
module.
random.
SystemRandom
([seed])Class that uses the os.urandom()
function for generating random numbers from sources provided by the operating system. Not available on all systems. Does not rely on software state, and sequences are not reproducible. Accordingly, the seed()
method has no effect and is ignored. The getstate()
and setstate()
methods raise NotImplementedError
if called.
Comments