Exploring the Python Random Module
The random module in Python provides a suite of functions for generating random numbers, shuffling sequences, and performing other random operations. In this guide, we will explore how to use the random module to generate random numbers, select random items, shuffle lists, and more.
Importing the Random Module
Before using any of the functions in the random module, we need to import it into our Python program. The random module is part of Python's standard library, so you don't need to install anything extra—just import it and start using it.
# To import the random module
import random
# Now you can use any function from the random module, like random.randint() or random.choice()
# To import the random module
import random
# Now you can use any function from the random module, like random.randint() or random.choice()
Once the random module is imported, you can call its functions using the random. prefix. For example, to generate a random integer, you would write random.randint(1, 10), or to pick a random item from a list, you can use random.choice(my_list).
Here's an example of how to generate a random number between 1 and 100 using the random.randint() function:
import random
# Generate a random integer between 1 and 100
random_number = random.randint(1, 100)
print(random_number) # Output: A random integer between 1 and 100
import random
# Generate a random integer between 1 and 100
random_number = random.randint(1, 100)
print(random_number) # Output: A random integer between 1 and 100
As shown above, once the module is imported, you can use it freely throughout your code. The random module provides a variety of functions for different randomization tasks.
Random Module Functions
The random module contains several functions for generating random numbers and manipulating sequences. Below are some of the most commonly used functions:
Function | Description |
---|---|
randint() | Returns a random integer between two given integers (inclusive) |
random() | Returns a random float between 0 and 1 |
choice() | Returns a random element from a non-empty sequence |
shuffle() | Shuffles the elements of a list in place |
sample() | Returns a random sample of elements from a population |
uniform() | Returns a random floating-point number between two given values |
seed() | Sets the seed for the random number generator for reproducibility |
randrange() | Returns a randomly selected element from the range(start, stop[, step]) |
triangular() | Returns a random float in the range [left, right] with a triangular distribution |
betavariate() | Returns a random float from the Beta distribution |
expovariate() | Returns a random float from the Exponential distribution |
gammavariate() | Returns a random float from the Gamma distribution |
gauss() | Returns a random float from the Gaussian distribution (normal distribution) |
lognormvariate() | Returns a random float from the Lognormal distribution |
normalvariate() | Returns a random float from the Normal distribution |
vonmisesvariate() | Returns a random float from the Von Mises distribution |
paretovariate() | Returns a random float from the Pareto distribution |
weibullvariate() | Returns a random float from the Weibull distribution |
Examples of Random Functions in Python:
import random
# Example 1: randint()
random_int = random.randint(1, 10) # Returns a random integer between 1 and 10
print(random_int) # Output: (random integer between 1 and 10)
# Example 2: random()
random_float = random.random() # Returns a random float between 0 and 1
print(random_float) # Output: (random float between 0 and 1)
# Example 3: choice()
fruits = ['apple', 'banana', 'cherry', 'date']
random_fruit = random.choice(fruits) # Returns a random item from the list
print(random_fruit) # Output: (random fruit from the list)
# Example 4: shuffle()
deck = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
random.shuffle(deck) # Shuffles the deck in place
print(deck) # Output: (shuffled deck)
# Example 5: sample()
sample_fruits = random.sample(fruits, 2) # Returns a random sample of 2 fruits from the list
print(sample_fruits) # Output: (random sample of 2 fruits)
# Example 6: uniform()
random_float_between = random.uniform(1.5, 5.5) # Returns a random float between 1.5 and 5.5
print(random_float_between) # Output: (random float between 1.5 and 5.5)
# Example 7: seed()
random.seed(42) # Sets the seed for reproducibility
random_int = random.randint(1, 10) # This will always return the same result with seed 42
print(random_int) # Output: (random integer based on seed)
# Example 8: randrange()
random_range = random.randrange(10, 20, 2) # Returns a random number between 10 and 20 with a step of 2
print(random_range) # Output: (random number between 10 and 20 with step 2)
# Example 9: triangular()
random_triangle = random.triangular(1, 10, 5) # Returns a random float based on triangular distribution
print(random_triangle) # Output: (random float based on triangular distribution)
# Example 10: betavariate()
random_beta = random.betavariate(2, 5) # Returns a random float from the Beta distribution
print(random_beta) # Output: (random float based on Beta distribution)
# Example 11: expovariate()
random_expo = random.expovariate(1) # Returns a random float from the Exponential distribution
print(random_expo) # Output: (random float based on Exponential distribution)
# Example 12: gammavariate()
random_gamma = random.gammavariate(2, 2) # Returns a random float from the Gamma distribution
print(random_gamma) # Output: (random float based on Gamma distribution)
# Example 13: gauss()
random_gaussian = random.gauss(0, 1) # Returns a random float based on the Gaussian distribution
print(random_gaussian) # Output: (random float based on Gaussian distribution)
# Example 14: lognormvariate()
random_lognormal = random.lognormvariate(0, 1) # Returns a random float based on LogNormal distribution
print(random_lognormal) # Output: (random float based on LogNormal distribution)
# Example 15: normalvariate()
random_normal = random.normalvariate(0, 1) # Returns a random float based on Normal distribution
print(random_normal) # Output: (random float based on Normal distribution)
# Example 16: vonmisesvariate()
random_vonmises = random.vonmisesvariate(0, 1) # Returns a random float based on Von Mises distribution
print(random_vonmises) # Output: (random float based on Von Mises distribution)
# Example 17: paretovariate()
random_pareto = random.paretovariate(2) # Returns a random float based on Pareto distribution
print(random_pareto) # Output: (random float based on Pareto distribution)
# Example 18: weibullvariate()
random_weibull = random.weibullvariate(1, 1) # Returns a random float based on Weibull distribution
print(random_weibull) # Output: (random float based on Weibull distribution)
import random
# Example 1: randint()
random_int = random.randint(1, 10) # Returns a random integer between 1 and 10
print(random_int) # Output: (random integer between 1 and 10)
# Example 2: random()
random_float = random.random() # Returns a random float between 0 and 1
print(random_float) # Output: (random float between 0 and 1)
# Example 3: choice()
fruits = ['apple', 'banana', 'cherry', 'date']
random_fruit = random.choice(fruits) # Returns a random item from the list
print(random_fruit) # Output: (random fruit from the list)
# Example 4: shuffle()
deck = ['Ace', '2', '3', '4', '5', '6', '7', '8', '9', '10', 'J', 'Q', 'K']
random.shuffle(deck) # Shuffles the deck in place
print(deck) # Output: (shuffled deck)
# Example 5: sample()
sample_fruits = random.sample(fruits, 2) # Returns a random sample of 2 fruits from the list
print(sample_fruits) # Output: (random sample of 2 fruits)
# Example 6: uniform()
random_float_between = random.uniform(1.5, 5.5) # Returns a random float between 1.5 and 5.5
print(random_float_between) # Output: (random float between 1.5 and 5.5)
# Example 7: seed()
random.seed(42) # Sets the seed for reproducibility
random_int = random.randint(1, 10) # This will always return the same result with seed 42
print(random_int) # Output: (random integer based on seed)
# Example 8: randrange()
random_range = random.randrange(10, 20, 2) # Returns a random number between 10 and 20 with a step of 2
print(random_range) # Output: (random number between 10 and 20 with step 2)
# Example 9: triangular()
random_triangle = random.triangular(1, 10, 5) # Returns a random float based on triangular distribution
print(random_triangle) # Output: (random float based on triangular distribution)
# Example 10: betavariate()
random_beta = random.betavariate(2, 5) # Returns a random float from the Beta distribution
print(random_beta) # Output: (random float based on Beta distribution)
# Example 11: expovariate()
random_expo = random.expovariate(1) # Returns a random float from the Exponential distribution
print(random_expo) # Output: (random float based on Exponential distribution)
# Example 12: gammavariate()
random_gamma = random.gammavariate(2, 2) # Returns a random float from the Gamma distribution
print(random_gamma) # Output: (random float based on Gamma distribution)
# Example 13: gauss()
random_gaussian = random.gauss(0, 1) # Returns a random float based on the Gaussian distribution
print(random_gaussian) # Output: (random float based on Gaussian distribution)
# Example 14: lognormvariate()
random_lognormal = random.lognormvariate(0, 1) # Returns a random float based on LogNormal distribution
print(random_lognormal) # Output: (random float based on LogNormal distribution)
# Example 15: normalvariate()
random_normal = random.normalvariate(0, 1) # Returns a random float based on Normal distribution
print(random_normal) # Output: (random float based on Normal distribution)
# Example 16: vonmisesvariate()
random_vonmises = random.vonmisesvariate(0, 1) # Returns a random float based on Von Mises distribution
print(random_vonmises) # Output: (random float based on Von Mises distribution)
# Example 17: paretovariate()
random_pareto = random.paretovariate(2) # Returns a random float based on Pareto distribution
print(random_pareto) # Output: (random float based on Pareto distribution)
# Example 18: weibullvariate()
random_weibull = random.weibullvariate(1, 1) # Returns a random float based on Weibull distribution
print(random_weibull) # Output: (random float based on Weibull distribution)
What's Next?
Next, you'll learn about the Python time module, which provides various functions to work with time-related tasks. You'll discover how to measure time, format timestamps, and pause your program for a specified duration. This module is essential for tasks like performance measurement and scheduling.