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

  1. random()
    • Generates a random float between 0.0 and 1.0.
    import random
    print(random.random())  # Example output: 0.637148509878836
    
  2. randint(a, b)
    • Returns a random integer between a and b (inclusive).
    print(random.randint(1, 10))  # Example output: 7
    
  3. randrange(start, stop, step)
    • Returns a randomly selected element from range(start, stop, step).
    print(random.randrange(1, 10, 2))  # Example output: 3
    
  4. 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'
    
  5. choices(population, weights=None, k=1)
    • Returns a list of k random elements from the population, 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']
    
  6. 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]
    
  7. sample(population, k)
    • Returns a list of k unique random elements from the population (i.e., without replacement).
    print(random.sample([1, 2, 3, 4, 5], 3))  # Example output: [1, 5, 3]
    
  8. uniform(a, b)
    • Returns a random float N such that a <= N <= b.
    print(random.uniform(1.0, 10.0))  # Example output: 6.572802728059156
    
  9. triangular(low, high, mode)
    • Returns a random float from the triangular distribution between low and high with mode as the mode (most frequent value).
    print(random.triangular(1.0, 10.0, 5.0))  # Example output: 4.56512931772688
    
  10. gauss(mu, sigma)
    • Returns a random float from the Gaussian distribution with mean mu and standard deviation sigma.
    print(random.gauss(0, 1))  # Example output: -0.5281203197372714
    

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 a is omitted or None, it uses the current system time.
    random.seed(42)
    print(random.random())  # Example output: 0.6394267984578837
    

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.

Leave a Reply 0

Your email address will not be published. Required fields are marked *