Python 5a - Random

Importing

This section looks at additional commands that you can import and use from Python’s code library.

 

A library is a collection of different coding sequences 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 Python. For example in the Sleeping page below we will want to open up Python’s library, go to the section called time and grab just the command called sleep. This is more efficient than just importing the whole time section.

Random Numbers

To use the random command we must first import the random section from Python’s code library on the first line of the program:

To generate a random number you need to use the random.randint command (randint stands for random integer).

 

After this command, in brackets, declare the range of numbers to choose from, e.g. (1,20) or (100,1000) and save it into a variable.

Above is a valid random number generation line but it needs the import command and a print line to be a complete program:

The two values in the brackets of a random.randint command denote the upper and lower limits of the random selection.

 

They do not have to be fixed values however and could be replaced by variables.

 

Below is a program where the user selects the upper and lower values of the range:

Practice Task 1

1. Generate a random number between 1 and 5.

2. Ask the user to enter a number between 1 and 5.

3. If the user’s number equals the random number then print “CORRECT!” otherwise print “INCORRECT!”

Example solution:

Random Characters

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

 

Just like before, you need to specify the range to select from in brackets after the command.

Below is a program that randomly chooses a letter of the alphabet:

Of course, the range of characters to choose from does not have to be solely letters, you could use numbers or punctuation.

 

Also, the range does not have to be fixed either, you could allow the user to enter data as a variable and select from that instead, such as below:

Practice Task 2

1. Generate a random sample from “aeiou”.

2. Ask the user to input a vowel (make sure they use lowercase letters too).

3. If the user’s vowel matches up with the random sample then output an appropriate response.

4. Print a different response if they do not match up.

Example solution:

Random Sample

To choose more than one value from a set of data you could use the command: random.sample

After you have written this command you need to specify the list of values to sample from and how many values you want to choose:

Be careful with your brackets.

 

The string values must be within speech marks and separated by commas and enclosed within square brackets.

 

After the list of string values you need another comma and then a value to denote how many values to select.

Below is a program that selects three values from a list of strings and outputs them, remember to include import random at the start for every program using a random command.

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

 

By implementing the range command too, you don’t need to individually write out each value like the string example above.

Practice Task 3

Your aunt is going to have a baby boy!

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

Example solution:

© CSNewbs 2019