Random Module

Course- Python >

Python offers random module that can generate random numbers. These are pseudo-random number as the sequence of number generated depends on the seed. If the seeding value is same, the sequence will be the same. For example, if you use 2 as the seeding value, you will always see the following sequence.

>>> import random
>>> random.seed(2)
>>> random.random()
0.9560342718892494
>>> random.random()
0.9478274870593494
>>> random.random()
0.05655136772680869

Not so random eh? Since this generator if completely deterministic, it must not be used for encryption purpose.

 

 
 

Here is the list of all the functions defined in random module with a brief explanation of what they do.

List of Functions in Python Random Module
Function Description
seed(a=None, version=2) Initialize the random number generator
getstate() Returns an object capturing the current internal state of the generator
setstate(state) Restores the internal state of the generator
getrandbits(k) Returns a Python integer with k random bits
randrange(start, stop[, step]) Returns a random integer from the range
randint(a, b) Returns a random integer between a and b inclusive
choice(seq) Return a random element from the non-empty sequence
shuffle(seq) Shuffle the sequence
sample(population, k) Return a k length list of unique elements chosen from the population sequence
random() Return the next random floating point number in the range [0.0, 1.0)
uniform(a, b) Return a random floating point number between a and b inclusive
triangular(low, high, mode) Return a random floating point number between low and high, with the specified mode between those bounds
betavariate(alpha, beta) Beta distribution
expovariate(lambd) Exponential distribution
gammavariate(alpha, beta) Gamma distribution
gauss(mu, sigma) Gaussian distribution
lognormvariate(mu, sigma) Log normal distribution
normalvariate(mu, sigma) Normal distribution
vonmisesvariate(mu, kappa) Vonmises distribution
paretovariate(alpha) Pareto distribution
weibullvariate(alpha, beta) Weibull distribution