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.
#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.
#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.
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.
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.
#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.
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 as shown below.
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.
