Skip to content

math.random

random numbers, picks, shuffles and dice, chances in plain words, and fortunes for fun

Random results come from a generator you can seed to repeat a run. Besides numbers, picks and shuffles, there are dice, coin flips, probability questions, and a few fortune-telling toys.

Tags: random

  • rand a random decimal from 0 up to but not including 1
  • seed make the session’s random choices repeatable
  • choice a random item from the list
  • shuffle a copy of the list in random order
  • roll roll dice written as in tabletop games, like “3d6+2”, and return {total, rolls}
  • birthday_paradox the chance that at least two of n people share a birthday
  • odds a probability as “1 in N”, with a familiar event that’s about as likely, to give a feel for it
  • fortune a random fortune-cookie saying
  • eight_ball an answer from a Magic 8-Ball toy
  • coin “heads” or “tails”, or a list of n flips
  • yes_or_no a random “yes” or “no”, a little more likely to be yes on Fridays
  • excuse a random excuse for why the code doesn’t work
rand() -> float / rand(a: int, b: int) -> int / rand(a: num, b: num) -> float

a random decimal from 0 up to but not including 1

With a and b, a random number from a to b, including both: whole numbers if both are integers, otherwise decimals

1
rand()0.85518
2
rand(1, 6)6
Example outputCtrl+Enter to run

See also choice, shuffle

seed(n: int) -> nil

make the session’s random choices repeatable

After the same seed, rand, shuffle, maze and the rest give the same results again

1
seed(7); rand(1, 100)78
Example outputCtrl+Enter to run

See also rand

choice(xs: list) -> any

a random item from the list

1
["rock", "paper", "scissors"].choice"rock"
Example outputCtrl+Enter to run

See also rand, shuffle

shuffle(xs: list) -> list

a copy of the list in random order

1
(1..6).shuffle[5, 4, 1, 3, 2]
Example outputCtrl+Enter to run

See also choice

roll(dice: str = “1d6”) -> map

roll dice written as in tabletop games, like “3d6+2”, and return {total, rolls}

“3d6+2” is three six-sided dice plus 2, “d20” one twenty-sided die, “d%” a percentile die, “4d6kh3” keeps the highest 3 of 4 dice and “2d20kl1” the lowest 1

1
roll("3d6+2"){total: 18, rolls: [5, 6, 5]}
2
roll("d%").total81
Example outputCtrl+Enter to run

See also rand, coin

birthday_paradox(people: int, days: int = 365) -> float

the chance that at least two of n people share a birthday

Give days to ask the same about any number of equally likely values, like hash collisions

1
birthday_paradox(23)0.507297
2
birthday_paradox(70)0.99916
3
birthday_paradox(10, 100)0.371843
Example outputCtrl+Enter to run

See also odds, choose

odds(p: num) -> str

a probability as “1 in N”, with a familiar event that’s about as likely, to give a feel for it

1
odds(1 / 1000)"1 in 1,000: about as likely as flipping ten heads in a row (1 in 1,024)"
2
odds(0.5)"1 in 2: about as likely as a coin landing heads (1 in 2)"
3
odds(2 ** -64)"1 in 1.84 × 10^19: about as likely as a perfect March Madness bracket (1 in 9.20 × 10^18)"
Example outputCtrl+Enter to run

See also birthday_paradox

fortune() -> str

a random fortune-cookie saying

1
fortune()"You will be asked to help someone move. Have an excuse ready."
Example outputCtrl+Enter to run

See also eight_ball

eight_ball(question?: str) -> str

an answer from a Magic 8-Ball toy

Asking the same question again the same day gets the same answer

1
eight_ball("is it Friday?")"Signs point to yes."
Example outputCtrl+Enter to run

See also yes_or_no, fortune

coin(n?: int) -> str|list

“heads” or “tails”, or a list of n flips

1
coin()"tails"
2
coin(3)["heads", "tails", "tails"]
Example outputCtrl+Enter to run

See also rand

yes_or_no(question?: str) -> str

a random “yes” or “no”, a little more likely to be yes on Fridays

1
yes_or_no()"yes"
Example outputCtrl+Enter to run

See also eight_ball

excuse() -> str

a random excuse for why the code doesn’t work

1
excuse()"I'm not late, everyone else is early."
Example outputCtrl+Enter to run

See also fortune

1
# roll a die
2
rand(1, 6)4
3
# a decimal between 1 and 2
4
rand(1.0, 2.0)1.03496
5
# pick one
6
["rock", "paper", "scissors"].choice"scissors"
7
# shuffle
8
(1..=5).shuffle[2, 4, 1, 5, 3]
9
# a D&D stat: 4d6, keep the best 3
10
roll("4d6kh3"){total: 13, rolls: [6, 4, 3, 2]}
11
# roll 2d20, keep the higher, add 5
12
roll("2d20kh1+5").total23
13
# five coin flips
14
coin(5)["tails", "tails", "tails", "tails", "heads"]
15
# the chance two of 23 people share a birthday
16
birthday_paradox(23)0.507297
17
# how unlikely is the jackpot?
18
odds(1 / 292201338)"1 in 292,201,338: about as likely as winning the Powerball jackpot (1 in 292,201,338)"
19
# ask the ball
20
eight_ball("should I get a dog?")"Outlook good."
21
# why you're late
22
excuse()"My socks didn't match and I couldn't go on."
Example outputCtrl+Enter to run

Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.