Python Random Module
Python Random Module
The Python random module is a built-in Python module used to generate random numbers. These are pseudorandom numbers, meaning they are not truly random. This module can be used to perform random operations, such as generating random numbers and printing random values from a list or string.
Example: Printing a random value from a list
# import random
import random
# prints a random value from the list
list1 = [1, 2, 3, 4, 5, 6]
print(random.choice(list1))
Output:
2
As mentioned above, the random module creates pseudorandom numbers. The random numbers depend on the seed value. For example, if the seed value is 5, the output of the following program will always be the same.
Example: Creating Random Numbers Using a Seed Value
import random
random.seed(5)
print(random.random())
print(random.random())
Output:
0.6229016948897019
0.7417869892607294
The output of the above code will always be the same. Therefore, it cannot be used for encryption.
Let’s discuss some common operations performed by this module.
Creating Random Integers
The random.randint() method is used to generate a random integer within a specified range.
Syntax:
randint(start, end)
Example: Create random integers
# Python3 program explaining work
# of randint() function
# import random module
import random
#Generates a random number between
# a given positive range
r1 = random.randint(5, 15)
print("Random number between 5 and 15 is % s" % (r1))
#Generates a random number between
# two given negative range
r2 = random.randint(-10, -2)
print("Random number between -10 and -2 is % d" % (r2))
Output:
Random number between 5 and 15 is 7
Random number between -10 and -2 is -9
Creating Random Floating-Point Numbers
The random.random() method generates a random floating-point number between 0.0 and 1.
Syntax:
random.random()
Example:
# Python 3 program to demonstrate
# the use of the random() function.
# import random
from random import random
# Prints a random item
print(random())
Output:
0.3717933555623072
Choose a Random Element
The random.choice() function returns a random item from a list, tuple, or string.
Syntax:
random.choice(sequence)
Example: Selecting Random Elements from Lists, Strings, and Tuples
# Python 3 program to demonstrate the use of
# choice() method
# import random
import random
# prints a random value from the list
list1 = [1, 2, 3, 4, 5, 6]
print(random.choice(list1))
# prints a random item from the string
string = "geeks"
print(random.choice(string))
# prints a random item from the tuple
tuple1 = (1, 2, 3, 4, 5)
print(random.choice(tuple1))
Output:
2
k
5
Shuffle a List
The random.shuffle() method is used to shuffle a sequence (list). Shuffling means changing the position of elements in a sequence. Here, the shuffle operation is performed in-place.
Syntax:
random.shuffle(sequence, function)
Example: shuffling the list
# import the random module
import random
# declare a list
sample_list = [1, 2, 3, 4, 5]
print("Original list : ")
print(sample_list)
# first shuffle
random.shuffle(sample_list)
print("nAfter the first shuffle : ")
print(sample_list)
# second shuffle
random.shuffle(sample_list)
print("nAfter the second shuffle : ")
print(sample_list)
Output: