- 0shares
- Facebook0
- Twitter0
- Google+0
- Pinterest0
- LinkedIn0
Python Random Module
Random functions in a program can be used by importing the random module. A random module is used to generate random numbers. These randomly generated numbers are the pseudo random numbers because they depend on seeds.
As the numbers that are generated randomly by the random module are depended on seeds, therefore, when the values of seeds are same then the sequence will also be same. For example if we have 3 as the seeding value then we will have the following sequence:
CODE:
import random
random. seed(3)
print (random. random ())
print(random. random())
print(random. random())
OUTPUT:
0.23796462709189137
0.5442292252959519
0.36995516654807925
It can be seen in the above example that the output will be in a sequence.
Consider the following table which is the list of all the functions that are defined in the random module:
Function | Description |
seed (a = None, version =2) | This function is used to initialize random number generator. |
getstat () | This function is used to return an object that is capturing the internal state of the generator; the state will be current state. |
setstate (state) | This function is used restore the internal stat of the generator. |
getrandbits (k) | This function is used to return the k random bits of a Python number. |
randrange (start, stop [, step]) | This function is used to return a random integer from a range. The starting and ending point with the increment will be passed by the user. |
randint (a, b) | This function is used to return a random integer between a and b. |
choice (seq) | This function is used to return a random number from a non empty sequence. |
shuffle (seq) | This function is used to shuffle a non empty sequence. |
sample (population, k) | This function is used to return a unique list of elements from the sequence named population, the length of the list of elements will be k. |
random () | This function is used to return a random floating point number from the range [0.0, 1.0). |
uniform (a, b) | This function is used to return a random floating point number between a and b inclusive. |
triangular (low, high, mode) | This function is used to return a floating point number between low and high and with the specified mode between those bounds. |
betavariate (alpha, beta) | This function is used to return the beta distribution. |
expovariate (lambd) | This function is used to return the exponential distribution. |
gammavariate (alpha, beta) | This function is used to return the gamma distribution. |
gauss (mu, sigma) | This function is used to return the Gaussian distribution. |
lognormvariate (mu, sigma) | This function is used to return the normal distribution. |
vanmisesvariate (mu, kappa) | This function is used to return the Vonmises distribution. |
paretovariate (alpha) | This function is used to return Pareto distribution. |
weibullvariate (alpha, beta) | This function is used to return Weibull distribution. |