Skip to content

8.6 Random module with data structures

8.6a Overview of the Python random module

The Python random module is used to generate pseudo-random numbers for various distributions. It provides functions to generate random numbers, shuffle data, and select random elements from sequences like lists and tuples.

 


 

8.6b Using randint(start, stop)

The randint(start, stop) function returns a random integer N such that start <= N <= stop.

Example 8.6.1 - randint(start, stop)

import random

# Generate a random integer between 1 and 10 (inclusive)
random_number = random.randint(1, 10)
print(f"Random number between 1 and 10: {random_number}")

 


 

8.6c Using random module with data structures

 

  • choice(seq) - The choice(seq) function returns a randomly selected element from the non-empty sequence seq.

Example 8.6.1 - random choice with a list

import random

fruits = ['apple', 'banana', 'cherry', 'date', 'elderberry']

# Select a random fruit
random_fruit = random.choice(fruits)
print(f"Random fruit: {random_fruit}")

 

Example 8.6.2 - random choice with a tuple

import random

colors = ('red', 'green', 'blue', 'yellow', 'purple')

# Select a random color
random_color = random.choice(colors)
print(f"Random color: {random_color}")

 

  • shuffle(seq) - The shuffle(seq) function randomly shuffles the elements of the list seq in place. Note that it works only with lists, not tuples.

Example 8.6.3 - random shuffle a list

import random

numbers = [1, 2, 3, 4, 5]

# Shuffle the list
random.shuffle(numbers)
print(f"Shuffled numbers: {numbers}")

 

  • sample(seq, k) - The sample(seq, k) function returns a new list containing k unique elements randomly chosen from the sequence seq.

Example 8.6.4 - random sample from a list

import random

letters = ['a', 'b', 'c', 'd', 'e', 'f']

# Select 3 random unique letters
random_letters = random.sample(letters, 3)
print(f"Random letters: {random_letters}")

 

Example 8.6.5 - random sample from a tuple

import random

animals = ('cat', 'dog', 'elephant', 'giraffe', 'horse')

# Select 2 random unique animals
random_animals = random.sample(animals, 2)
print(f"Random animals: {random_animals}")

 


 

8.6d Random module summary

The Python random module provides functions for generating random numbers and manipulating data structures.

  • randint(start, stop) - Generates a random integer between start and stop.
  • choice(seq) - Selects a random element from a sequence (list or tuple).
  • shuffle(seq) - Randomly shuffles a list in place.
  • sample(seq, k) - Returns a list of k unique elements randomly chosen from a sequence (list or tuple).

By using these functions, you can easily generate random values and manipulate data structures in various ways.

 


 

8.6e Develop a program using random module

Exercise 8.6.1 - Create a small guessing game using the Python random module and decision statements. The game should follow these steps:

  1. Generate a random number between 1 and 20.
  2. Ask the user to guess the number.
  3. Provide feedback if the guess is too low, too high, or correct.
  4. Allow the user to keep guessing until they get the correct number.
  5. Print the number of attempts it took to guess the correct number.

Exercise 8.6.1 - Model Answer - Make sure to work out the exercise before checking the model answer.