Python 5a – Random

Generate random numbers, pick random items, and take random samples to power games and simulations.

Importing from Modules

Python ships with batteries included. You can import parts of a module you need:

from random import randint, choice, sample, shuffle
from time import sleep, ctime, strftime

Random Integers with randint()

randint(a, b) returns an integer N such that a ≤ N ≤ b (inclusive at both ends):

from random import randint
number = randint(1, 100)
print("A random number between 1 and 100 is", number)

Let users choose the range:

from random import randint
lower = int(input("What is the lowest number? "))
upper = int(input("What is the highest number? "))
number = randint(lower, upper)
print("A random number between", lower, "and", upper, "is", number)

Random Numbers Task 1 – Ice Comet

Print a random year from the current year up to 2099:

from random import randint
import datetime

current_year = datetime.datetime.now().year
year = randint(current_year, 2099)
print("Did you know the ice comet will next pass Earth in", year, "?")

Random Numbers Task 2 – Guess the Number

Generate a number 1–5 and let the user guess it:

from random import randint
number = randint(1, 5)
guess = int(input("Enter a number between 1 and 5: "))
print("Computer's number:", number)
if guess == number:
    print("Well guessed! It's a match!")
else:
    print("No match this time!")

Random Choice with choice()

choice(seq) picks one random element from a list or string:

from random import choice
animals = ["cat", "dog", "horse", "cow"]
random_animal = choice(animals)
print("A random animal is", random_animal)

Or pick a random letter:

from random import choice
letters = "abcdefghijklmnopqrstuvwxyz"
random_letter = choice(letters)
print("A random letter is", random_letter)

Random Choice Task 1 – Holiday Destinations

Help Harriet choose a destination by picking one at random:

from random import choice
destinations = ["Paris", "Barcelona", "Tokyo", "New York", "Sydney", "Rome"]
print("Why don't you go to", choice(destinations), "on holiday?")

Random Choice Task 2 – Vowels

Pick a random vowel and see if the user matches it:

from random import choice
vowels = "aeiou"
random_vowel = choice(vowels)
user_vowel = input("Enter a vowel: ")
print("Random vowel:", random_vowel)
if user_vowel == random_vowel:
    print("The vowels matched!")
else:
    print("The vowels didn't match!")

Random Samples with sample()

sample(population, k) returns k unique elements chosen at random (no repeats):

from random import sample
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
two_days = sample(days, 2)
print("You will be set homework on:", *two_days)

Sample several numbers from a range:

from random import sample
numbers = sample(range(1, 100), 5)
print("Five random numbers between 1 and 100 are:", *numbers)

Random Samples Task 1 – Frost Comets

Print four random years from 2023 to 2095 (inclusive):

from random import sample
years = sample(range(2023, 2096), 4)
print("I predict the frost comets will be seen in these years:", *years)

Random Samples Task 2 – Baby Boy Names

Suggest three baby names at random from a list of ten:

from random import sample
names = ["Charlie", "Eddie", "Frank", "George", "Harold", "Bill", "Tom", "Sam", "Joe", "Harry"]
suggestions = sample(names, 3)
print("Hey Aunt Meredith, how about these names:", *suggestions)