replit

Before you start this course, you must login to Replit

If you have forgotten your Replit account user name or password, ask your Coder Coach.

CodeSports

5.6 Random Investigate Overview

This unit will focus on generating and using random numbers.

It is important to include the import random at the top of your code to be able to generate and use random numbers

Instructions


Examine the example code in main and answer the questions below. Make sure to write your answers with comments (#)

Generating Random Numbers


Numbers are randomly generated within a range of numbers. To generate them, use:

random.randint(min,max)

The min is the lowest the random number can be, and the max is the highest the random number can be.

Random numbers can also be stored in variables, like this:

num = random.randint(1,5)

This means that num can be a random number between 1 and 5

Help! My Code Does Not Work!


When you run into errors in your code, check if:

  • import random missing from the top of the program
  • import random used every time a random number is generated (it only needs to be done once)
  • spelling of ‘randint’
  • max and min random values in brackets after ‘randint’
  • comma between the max and min random values in brackets after ‘randint’

Hints