top of page
top

Python 5a - Random

noun-import-1073974-FFFFFF.png

Importing

Section 5 looks at additional commands that you can import and use from Python’s code libraries.

​

A library is a collection of different commands that automatically come with Python but are separate from the main file. They can be imported (brought in) to your program by using the import command at the start of your program.

​

Imagine Python’s library to be similar to an actual library. There are different sections in a real library (such as History, Geography, Reference) and different sections in Python’s library (such as random or time). Each real library has many individual books in each section, just like commands in Python.

randint()

choice()

sample()

shuffle()

random

sleep()

ctime()

strftime()

time

from random import randint

from time import ctime

You can import a specific command from one of Python's libraries using the from and import commands at the top of your program.

Random Numbers

To generate random numbers, first import the randint command section from Python’s random code library on the first line of the program.

​

The randint command stands for random integer. In brackets, state the number range to randomly choose from.

​

The random value should be saved into a variable.

from random import randint

​

number = randint(1,100)

print("A random number between 1 and 100 is" , number)

=

A random number between 1 and 100 is 39

=

A random number between 1 and 100 is 73

=

A random number between 1 and 100 is 4

The randint range does not have to be fixed values and could be replaced by variables.

​

Below is a program where the user selects the upper and lower values of 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)

=

What is the lowest number? 1
What is the highest number? 50
A random number between 1 and 50 is 36

=

What is the lowest number? 500
What is the highest number? 1000
A random number between 500 and 1000 is 868

Random Numbers Task 1 (Ice Comet)

A special comet made of ice passes the Earth only once every one hundred years, and it hasn't been seen yet in the 21st century.

 

Use the randint command to randomly print a year between the current year and 2099.

Example solutions:

Did you know it won't be until 2032 that the ice comet will next pass Earth!?

Did you know it won't be until 2075 that the ice comet will next pass Earth!?

Random Numbers Task 2 (Guess the Number)

Use randint to generate a random number between 1 and 5.

​

Ask the user to enter a guess for the number with int and input.

 

Print the random number and use an if statement to check if there is a match, printing an appropriate statement if there is and something different if there is not a match.

Example solutions:

Enter a number between 1 and 5: 4
Computer's number: 5
No match this time!

Enter a number between 1 and 5: 3
Computer's number: 3
Well guessed! It's a match!

Choice - Random Word

Rather than just numbers, we can also randomly generate characters or strings from a specified range by using the choice command.

​

You must first import the choice command from the random library. Choice works well with a list of values, which require square brackets and commas separating each word.

​

Below is a program that randomly chooses from a list of animals:

from random import choice

​

animals = ["cat" , "dog" , "horse" , "cow"]

​

print("A random animal is" , choice(animals))

=

A random animal is cat

=

A random animal is horse

Choice - Random Character

Instead of using a list you can randomly select a character from a string.

​

The program below randomly selects a character from the variable named 'letters' which is the alphabet.

from random import choice

​

letters = "abcdefghijklmnopqrstuvwxyz"

 

print("A random letter is" , choice(letters))

=

A random letter is e

=

A random letter is y

Random Choice Task 1 (Holiday Destinations)

Harriet can't decide where to go on holiday and needs help deciding. 

​

Make a list of at least 6 destinations (see the animal example above) and use the choice command (don't forget to import it from the random library) to print a random destination.

Example solutions:

Why don't you go to Paris on holiday?

Why don't you go to Barcelona on holiday?

Random Choice Task 2 (Vowels)

Use the choice command to randomly select a vowel (look at the alphabet example above).

​

Ask the user to input a vowel and use an if statement to check if the user's letter matches the randomly selected letter.

​

Print a suitable statement if they match and something else if they don't.

Example solutions:

Enter a vowel: i
Random vowel: i
The vowels matched!

Enter a vowel: o
Random vowel: u
The vowels didn't match!

Sample - Random Strings

To choose more than one value from a set of data, use the sample command.

​

Sample is used with a list of values and a number representing how many from that list to pick. The code sample(days,2) picks two random values from the list called days.

Both examples below perform the same task but, as with most code, there is no one way to solve a problem.

from random import sample

​

days = ["Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday"]
two_days = sample(days , 2)

 

print("You will be set homework on:" , *two_days)

A separate list and then a sample.

=

You will be set homework on: Thursday Monday

=

You will be set homework on: Friday Tuesday

from random import sample

​

two_days = sample(["Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday"] , 2)

 

print("You will be set homework on:" , *two_days)

The list and sample is combined on one line.

The sample command actually makes a new list with the number of values selected (e.g. ["Tuesday" , "Thursday"] in the examples above).

​

You can use an asterisk - * - directly before the sampled variable to print just the list values, otherwise the brackets and apostrophes will be printed too.

from random import sample

​

names = sample(["Bob" , "Ben" , "Jen" , "Ken"] , 2)

​

print("The names are:" , names)

from random import sample

​

names = sample(["Bob" , "Ben" , "Jen" , "Ken"] , 2)

​

print("The names are:" , *names)

The names are: ['Bob', 'Jen']

The names are: Bob Jen

Sample - Random Numbers

You can also use the sample command to choose several integers from a given range.

 

By implementing the range command you don’t need to individually write out each number.

from random import sample

​

numbers = sample(range(1,100) , 5)

​

print("Five random numbers between 1 and 100 are:" , *numbers)

Five random numbers between 1 and 100 are: 53 42 11 8 20

Five random numbers between 1 and 100 are: 74 52 51 1 6

Random Samples Task 1 (Frost Comets)

The ice comet from a previous task has broken up into four smaller frosty comets that could pass the Earth anytime from next year to the year 2095.

​

Print four random years in that range.

Example solutions:

I predict the frost comets will be seen in these years: 2093 2036 2027 2091

I predict the frost comets will be seen in these years: 2076 2033 2053 2085

Random Samples Task 2 (Baby Boy)

Aunt Meredith is having a baby boy.

 

Create a program that randomly selects 3 male names from a list of 10 possible names.

Example solutions:

Hey Aunt Meredith, how about these names: Charlie Eddie Frank

Hey Aunt Meredith, how about these names: George Harold Bill

bottom of page