Locked lesson.
About this lesson
How to create and use random numbers.
Exercise files
Download this lesson’s related exercise files.
Random Numbers.docx59.1 KB Random Numbers - Solution.docx
59.1 KB
Quick reference
Random Numbers
How to generate random numbers in Python.
When to use
Use this whenever you need to generate random numbers.
Instructions
To create a random number, import the random module:
import random
To create a random floating point number between 0 an 1:
print(random.random())
To create a floating point random number in a range from 1 to 100:
print(random.uniform(1,100))
To create a random integer between 1 and 100:
print(random.randint(1,100))
Hints & tips
- import random
- print(random.random())
- print(random.uniform(1,100))
- print(random.randint(1,100))
Lesson notes are only available for subscribers.