Python Random Module

Python has a built-in module that can be used to generate random numbers.

random The module has a set of methods as follows:

Method Description
seed() Initializes the random number generator.
getstate() Returns the current internal state of the random number generator.
setstate() Restores the internal state of the random number generator.
getrandbits() Returns a digit representing a random position.
randrange() Returns a random number between the given range.
randint() Returns a random number between the given range.
choice() Returns a random element from the given sequence.
choices() Returns a list containing random selections from the given sequence.
shuffle() Accepts a sequence and returns this sequence in a random order.
sample() Returns a given sample from the sequence.
random() Returns a floating-point number between 0 and 1.
uniform() Returns a random floating-point number between the two given parameters.
triangular() Returns a random floating-point number between the two given parameters, you can also set the mode parameter to specify the midpoint between the other two parameters.
betavariate() Returns a random floating-point number between 0 and 1 based on the Beta distribution (used in statistics).
expovariate() Returns a random floating-point number between 0 and 1 based on the exponential distribution (used in statistics), and between 0 and -1 if the parameter is negative.
gammavariate() Returns a random floating-point number between 0 and 1 based on the Gamma distribution (used in statistics).
gauss() Returns a random floating-point number between 0 and 1 based on the Gaussian distribution (used in probability theory).
lognormvariate() Returns a random floating-point number between 0 and 1 based on the log-normal distribution (used in probability theory).
normalvariate() Returns a random floating-point number between 0 and 1 based on the normal distribution (used in probability theory).
vonmisesvariate() Returns a random floating-point number between 0 and 1 based on the von Mises distribution (used in directional statistics).
paretovariate() Returns a random floating-point number between 0 and 1 based on the Pareto distribution (used in probability theory).
weibullvariate() Returns a random floating-point number between 0 and 1 based on the Weibull distribution (used in statistics).