Python random Module
The Python random module is mainly used to generate random numbers.
The random module implements pseudo-random number generators for various distributions.
To use the random functions, you must first import:
View the contents of the random module:
Example
Section titled “Example”Next, we use the random() method to return a random number in the half-open interval [0,1), including 0 but not including 1.
Example
Section titled “Example”The output of the above example is:
The seed() method changes the seed of the random number generator. You can call this function before calling other random module functions.
Example
Section titled “Example”The output after running the above example is:
random Module Methods
Section titled “random Module Methods”The random module methods are as follows:
| Method | Description |
| seed() | Initialize the random number generator |
| getstate() | Returns an object capturing the current internal state of the generator. |
| setstate() | state should have been obtained from a previous call to getstate(), and setstate() restores the internal state of the generator to what it was when getstate() was called. |
| getrandbits(k) | Returns a non-negative Python integer 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. |
| randrange() | Returns a randomly selected element from range(start, stop, step). |
| randint(a, b) | Returns a random integer N such that a <= N <= b. |
| choice(seq) | Returns a random element from the non-empty sequence seq. If seq is empty, raises IndexError. |
| choices(population, weights=None, *, cum_weights=None, k=1) | Returns a k-sized list of elements chosen from the population with replacement. If the population is empty, raises IndexError. |
| shuffle(x[, random]) | Randomly shuffles the sequence x in place. |
| sample(population, k, *, counts=None) | Returns a k-length list of unique elements chosen from the population sequence or set. Used for random sampling without replacement. |
| random() | Returns the next random floating-point number in the range [0.0, 1.0). |
| uniform() | Returns a random floating-point number N such that a <= N <= b when a <= b, and b <= N <= a when b < a. |
| triangular(low, high, mode) | Returns 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. |
| betavariate(alpha, beta) | Beta distribution. Conditions on the parameters are alpha > 0 and beta > 0. Returned values range between 0 and 1. |
| expovariate(lambd) | Exponential distribution. lambd is 1.0 divided by the desired mean. It should be nonzero. |
| gammavariate() | Gamma distribution. (Not the gamma function!) Conditions on the parameters are alpha > 0 and beta > 0. |
| gauss(mu, sigma) | Normal distribution, also called the Gaussian distribution. mu is the mean, and sigma is the standard deviation. This function is slightly faster than the normalvariate() function defined below. |
| lognormvariate(mu, sigma) | Log normal distribution. If you take the natural logarithm of this distribution, you will get a normal distribution with mean mu and standard deviation sigma. mu can be any value, and sigma must be greater than zero. |
| normalvariate(mu, sigma) | Normal distribution. mu is the mean, and sigma is the standard deviation. |
| vonmisesvariate(mu, kappa) | Von Mises 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. |
| paretovariate(alpha) | Pareto distribution. alpha is the shape parameter. |
| weibullvariate(alpha, beta) | Weibull distribution. alpha is the scale parameter and beta is the shape parameter. |