Python random and math
This page rolls one die, then lifts 2.1 up to the next integer. Both names already come with the language. You do not install random, and you do not install math. The first program fixes the roll so the lesson has one screen. random.seed(1) is written first. random.randint(1, 6) is the roll. A die has the faces 1 through 6. Both ends are real faces, so 1 can appear and 6 can appear. 0 is not a face, and 7 is not a face.
import random
random.seed(1)
print(random.randint(1, 6))
The screen shows 2. That 2 is one face of the die. It is not a fraction with the tail cut off, and this program never calls floor. random.seed(1) is why the face is 2. Run the same three lines again and the screen is 2 again. Take the seed line away and a later run can show a different face, still only from 1 through 6. The seed line has to sit above the roll. A seed written after print does not change the number already on the screen.
import math
print(math.ceil(2.1))
The screen shows 3. math.ceil(2.1) moves up to the next integer. 2.1 does not stay 2.1, it does not become 2, and the result is not 3.0. You see the integer 3, with no decimal point. This program does not import random, and it does not roll a die. 2.1 is only the number being lifted.
📊 What each name decides
| Name | What it decides |
|---|---|
| random.seed(1) | the same face each time this program runs |
| random.randint(1, 6) | an integer from 1 through 6, and 6 is included |
| math.ceil(2.1) | the integer 3, one step up from 2.1 |
Leaving 6 off the die is a mistake. randint(1, 6) includes 6. Reading ceil as a cut is the other mistake. 2.1 is not left as 2. None is not written in either program. Both names are already on the machine, because they come with the language. This page does not download them.
The next page is installing a package with pip. This page does not install anything. It only rolls one die with a fixed seed, then lifts 2.1.
One roll, then a lift
random.seed(1) and randint(1, 6) show 2. Faces run from 1 through 6, including 6. math.ceil(2.1) shows the integer 3. The next page is installing a package with pip.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top