math and random
In short: math adds the functions arithmetic operators don't cover —
floor/ceil round toward a fixed direction, sqrt and gcd do real number
theory. random produces numbers that look unpredictable but are actually computed
from a seed; call random.seed(n) first and every random.* call after it
produces the exact same sequence, every run, on every machine. That determinism is
not a side detail — it's what makes a "random" demo reproducible, and what a
predict block or an automated test absolutely needs. Skip the seed and you get
real randomness, which is perfect for a game and useless for anything that must be
checked against an exact answer.

Split a bill into whole rupees
Three friends split a ₹100 bill. Whole rupees only — no one wants to deal in paise for a group split:
// (and divmod) always rounds toward negative infinity — math.floor does
the same thing for a value that's already a float. That's not the same as int(),
which chops off the decimal part toward zero instead:
import math
print(math.floor(-1.5), int(-1.5))math.ceil rounds the other way — useful the moment "some left over" means you need
one more of something, like a bus:
Simplify a ratio
A recipe scales 18 eggs to 24 cups of flour, and the app wants to show that ratio in its simplest form. Dividing both sides by the first small number that comes to mind looks like a fix, but isn't always the whole fix:
a, b = 18, 24
a, b = a // 2, b // 2
print(a, b)math.gcd finds the greatest common divisor directly, so dividing by it always
reduces a ratio all the way in one step:
Shuffle a deck reproducibly for a demo
A card game needs a shuffled deck — genuinely random for a real player, but a demo recording or an automated test needs the same shuffle every time it runs:
Run that box again: same three lines, every time, because random.seed(1) was
called first. Delete the seed call and every run would differ — fine for a real
game, but then a predict block about the output would have no single right answer.
random.choice picks one item from a sequence — the seed fixes that pick too:
One more from math: sqrt is the one most people already expect, and it's a
plain function call, not an operator:
Quick practice
import random
random.seed(7)
a = random.randint(1, 100)
random.seed(7)
b = random.randint(1, 100)
print(a == b)The output should show a price one paisa under a whole rupee (₹249.99), not ₹250 unchanged. floor is the wrong tool here — replace it.
🎯 Your turn
Write seeded_dice_rolls(seed, n). Seed the random module with seed, then roll a
6-sided die n times (random.randint(1, 6) each time) and return the list of
results, in order.
seeded_dice_rolls(1, 5)→[2, 5, 1, 3, 1]seeded_dice_rolls(7, 0)→[]
Hint: call random.seed(seed) once, first. Then a list comprehension:
[random.randint(1, 6) for _ in range(n)].
Press ▶ Run and hit ✓ Check. ✅
