Python Random Module
The random module in Python provides functions to generate random numbers and perform random operations. It is useful for tasks like generating random numbers, selecting random items from a sequence, shuffling elements, and more.
Common Functions in the random Module
random()- Generates a random float between 0.0 and 1.0.
import random print(random.random()) # Example output: 0.637148509878836randint(a, b)- Returns a random integer between
aandb(inclusive).
print(random.randint(1, 10)) # Example output: 7- Returns a random integer between
randrange(start, stop, step)- Returns a randomly selected element from
range(start, stop, step).
print(random.randrange(1, 10, 2)) # Example output: 3- Returns a randomly selected element from
choice(seq)- Returns a random element from the non-empty sequence
seq(e.g., list, tuple, string).
my_list = ['apple', 'banana', 'cherry'] print(random.choice(my_list)) # Example output: 'banana'- Returns a random element from the non-empty sequence
choices(population, weights=None, k=1)- Returns a list of
krandom elements from thepopulation, allowing elements to be selected more than once. You can also provide weights to influence the probability of selection.
print(random.choices(['apple', 'banana', 'cherry'], weights=[2, 1, 1], k=3)) # Example output: ['apple', 'cherry', 'apple']- Returns a list of
shuffle(seq)- Shuffles the elements of
seq(list) in place.
my_list = [1, 2, 3, 4] random.shuffle(my_list) print(my_list) # Example output: [3, 1, 4, 2]- Shuffles the elements of
sample(population, k)- Returns a list of
kunique random elements from thepopulation(i.e., without replacement).
print(random.sample([1, 2, 3, 4, 5], 3)) # Example output: [1, 5, 3]- Returns a list of
uniform(a, b)- Returns a random float
Nsuch thata <= N <= b.
print(random.uniform(1.0, 10.0)) # Example output: 6.572802728059156- Returns a random float
triangular(low, high, mode)- Returns a random float from the triangular distribution between
lowandhighwithmodeas the mode (most frequent value).
print(random.triangular(1.0, 10.0, 5.0)) # Example output: 4.56512931772688- Returns a random float from the triangular distribution between
gauss(mu, sigma)- Returns a random float from the Gaussian distribution with mean
muand standard deviationsigma.
print(random.gauss(0, 1)) # Example output: -0.5281203197372714- Returns a random float from the Gaussian distribution with mean
Seeding the Random Number Generator
The random number generator can be seeded to produce reproducible results. This is useful when you want to repeat random operations with the same outcomes.
seed(a=None)- Initializes the random number generator with a seed. If
ais omitted orNone, it uses the current system time.
random.seed(42) print(random.random()) # Example output: 0.6394267984578837- Initializes the random number generator with a seed. If
Example Usage of the Random Module
import random
# Seed the random number generator
random.seed(10)
# Generate a random float between 0 and 1
print("Random float:", random.random())
# Generate a random integer between 5 and 15
print("Random integer:", random.randint(5, 15))
# Select a random element from a list
my_list = ['apple', 'banana', 'cherry', 'date']
print("Random choice:", random.choice(my_list))
# Shuffle the list
random.shuffle(my_list)
print("Shuffled list:", my_list)
# Sample two unique elements from the list
print("Random sample:", random.sample(my_list, 2))
The random module provides a versatile set of tools for generating random numbers, choosing random elements from collections, and performing random sampling, making it valuable in simulations, testing, and games.