Python Random Module

In python, we don’t have any specific functions like random() to generate random values. If you want to generate some random values, you need to import a built-in random module.

 

Following is the example of importing a built-in random module to generate the random number.

 

import random

#generate random value
print(random.random())

If you observe the above example, we imported the random module using the import keyword and used the random() method to generate a random number. Whenever you execute the above python program, you will get a random value between 0.0 to 1.0.

 

Like the random() method, the random module has different methods to generate random numbers based on your requirements.

Randint() Method

If you want to generate the random integer values between the defined range of values, you need to use randint() method.

 

Following is the example of importing a built-in random module to generate the random number between 2 to 10 in python.

 

import random

#generate a random value between 2 to 10
print(random.randint(2, 10))

In randint() method, the two parameters (start, end) are required. Whenever you execute the above python program, you will get a random value between 2 to 10.

Randrange() Method

Same as the randint() method, the randrange() method also will generate the random value between the specified range, but it will accept three parameters (start, stop, step).

 

The first parameter is to define the starting position and its optional. By default, it will consider 0 as the starting position. The second parameter to define the stop position and its required. The third parameter to specify the incrementation and its optional. By default, it's 1.

 

Following is an example of using the randrange() function to generate the random number based on python's defined range values.

 

import random

print(random.randint(2, 10))
print(random.randrange(2,20,5))
print(random.randrange(2,100,20))

The above random module program will generate the random numbers between the defined range values using the randrange() method. 

 

2
17
82

Seed() Method

If you want to generate the same random number when you execute the above program, you need to use the seed(n) method in the random module. Here, the value n should always be the same; if you want to change the random value, then change the n value.

 

Following is an example of the random module to generate the same random number in python.

 

import random

#Always generate the same random number
random.seed(5)
print(random.randint(2, 10))

The above program will always generate the same random number (6) between 2 and 10 based on the seed value. If you want a different random value, you can change the value of the seed() method.

Choice() Method

The random module choice() method is useful to return the randomly selected element from the specified sequence. The given sequence must be non-empty.

 

Following is the example of using the random module choice() method to get the randomly selected element from the given sequence in python.

 

import random

print(random.choice('Tutlane'))
print(random.choice([1, 3, 10, 40, 50, 60]))
print(random.choice((1, 3, 10, 40, 50, 60)))

The above random module example will return the result like as shown below.

 

n
40
1

The following table lists the different methods available in the random module to randomly generate the numbers, randomly pick the elements, etc., based on our requirements.

 

MethodDescription
random() It returns the random number between 0.0 and 1.0.
randint() It generates a random number between the specified range.
randrange() It will generate a random number between the specified range.
seed() It will return the same random number.
shuffle() It will randomly reorder the items in the sequence.
choice() It will return random elements from the specified sequence.
choices() It will return a list with random elements from the specified sequence.
sample() It will return a list with the defined number of random elements from the sequence.
getstate() It will return the internal state of a random number generator.
setstate() It will restore the internal state of a random number generator.
getrandbits() It will return a number that represents the random bits.
uniform() It will return a random float number between two given numbers.
triangular() It will return a random float number between two given numbers, you can also use a mode parameter to specify the midpoint between the two numbers.
betavariate() It will return a random number between 0.0 and 1.0 based on the beta distribution.
expovariate() It will return a random float number based on the exponential distribution.
gammavariate() It will return a random float number based on the gamma distribution.
gauss() It will return a random float number based on the gaussian distribution.
lognormvariate() It will return a random float number based on the log-normal distribution.
normalvariate() It will return a random float number based on the normal distribution.
vonmisesvariate() It will return a random float number based on the von mises distribution.
paretovariate() It will return a random float number based on the pareto distribution.
weibullvariate() It will return a random float number based on the weibull distribution.