Skip to content

Random numbers


Related course:

Random

The module named random can be used to generate random numbers in Python. To use a module you need to type import module. This loads all of the functions inside the module.

Type the program shown below and run it:

import random

# Create a random floating point number and print it.
print(random.random())

# pick a random whole number between 0 and 10.
print(random.randrange(0,10))

# pick a random floating point number between 0 and 10.
print(random.uniform(0,10))

Study drill

Try the exercises below

1. Make a program that creates a random number and stores it into x.
2. Make a program that prints 3 random numbers.

After completing these continue with the next exercise.

Answers

Strings
Keyboard input

Be First to Comment

    Leave a Reply