Python random · Exercises
Core Python

Python random Exercises

4 exercises that build on the Python random Module lesson, from easy to hard. Each one is checked in your browser when you press Check. Stuck? Use the hint before peeking at the solution.

What this lesson covers: The random module draws pseudo-random numbers and picks from a seeded generator, so you can simulate dice, shuffle and sample lists, and repeat exactly the same results whenever you fix the seed. By the end you can generate numbers and picks with random(), randint, choice, choices, sample and shuffle, make results repeatable with seed, keep a private generator with Random(), and know when to use secrets instead. You will practise: Roll the dice; Predict the random facts; Fix the raffle; Repeatable shuffles and dice.

0 / 4 completed

Python random practice exercises

1. Roll the dice

Write code Easy

Write a function roll_dice(count) that returns a list of count random die rolls. Every roll is a whole number from 1 to 6, and 6 must be possible. Use random.randint.

Your code
import random

def roll_dice(count):
    # return a list of count rolls, each from 1 to 6
    pass

random.seed(11)
print(roll_dice(5))

2. Predict the random facts

Predict the output Easy

Nothing here depends on lucky numbers: every line prints something you can work out. Type exactly what the program prints, then press Check.

Read this code
import random

random.seed(1)
first = random.randint(1, 6)
random.seed(1)
second = random.randint(1, 6)
print(first == second)

cards = ["A", "K", "Q"]
result = random.shuffle(cards)
print(result)
print(sorted(cards))
print(len(random.sample(range(10), 4)))
print(random.randint(5, 5))

3. Fix the raffle

Fix the bug Medium

pick_winners(names, count) should return count different names, but sometimes the same person wins twice. Fix the function. It must not change the names list it receives.

Your code
import random

def pick_winners(names, count):
    return random.choices(names, k=count)

random.seed(2)
print(pick_winners(["Asha", "Ben", "Cara", "Dev", "Eli"], 3))

4. Repeatable shuffles and dice

Write code Hard

Write two functions that never touch the shared random generator:

  • shuffled_copy(items, seed) returns a new list with the same items in a shuffled order. The same seed must always give the same order, and items must not change.
  • rolls_until_six(seed) rolls a die with rng.randint(1, 6) and returns how many rolls it took to get a 6 (a 6 on the first roll gives 1).

Both functions should create their own generator with random.Random(seed).

Your code
import random

def shuffled_copy(items, seed):
    # return a new shuffled list, using your own random.Random(seed)
    pass

def rolls_until_six(seed):
    # keep rolling rng.randint(1, 6) until a 6 appears, return the number of rolls
    pass

print(shuffled_copy(["A", "K", "Q", "J"], 1))
print(rolls_until_six(1))

Next lessonPython datetime Module

More Python random Module examples · All Python exercises