Random Numbers: Roll the Dice!
Why does Bit's dice always roll a six?
Real Python code
This is the code from the video. Read it out loud like a recipe, one line at a time!
import random
dice = random.randint(1, 6)
print("You rolled", dice)
Quiz time!
Can this code ever print 7?
import random
dice = random.randint(1, 6)
print(dice)
Random hides in games
Board games
Roll a dice to see how far you move
Who goes first?
Pick a random player to start
Surprise spots
A star pops up in a new place each time
Fixed or random?
set dice to 6
- Always the same number
- Same result every run
- Boring for a dice game
pick random 1 to 6
- A new number each run
- Both ends count: 1 and 6
- Python: random.randint(1, 6)
- Every number has a fair chance
Same code, new surprise every time!
Try it yourself!
In Scratch, make a dice sprite that says pick random 1 to 6 when you click it.
Bit is working on the next one: X and Y