math.random
random numbers and picks, dice, odds in plain words, fortunes, a magic 8-ball
Example results generated on 2026-10-08. Ones using
now,todayor randomness will differ when you run them: press Run on any example to run it in your browser, after editing it if you like.
Functions
| function | description |
|---|---|
rand() / rand(a: num, b: num) | float in [0, 1), or a number from a to b inclusive |
choice(xs: list) | random item |
shuffle(xs: list) | shuffled copy |
roll(dice?: str) | roll dice like “3d6+2”, “d20”, “4d6kh3” (keep highest 3) or “2d20kl1”; returns {total, rolls}. Default 1d6 |
birthday_paradox(people: int, days?: int) | the chance at least two of n people share a birthday (or any of days equally likely values) |
odds(p: num) | a probability as “1 in N”, next to a familiar event about as likely |
fortune() | a fortune cookie |
eight_ball(question?: str) | a magic 8-ball answer; the same question gets the same answer all day |
coin(n?: int) | heads or tails, or a list of n flips |
yes_or_no(question?: str) | yes or no; leans yes on Fridays |
excuse() | why it doesn’t work |
rand
rand() / rand(a: num, b: num): float in [0, 1), or a number from a to b inclusive
rand()
# → 0.141038
rand(1, 6)
# → 5
choice
choice(xs: list): random item
["rock", "paper", "scissors"].choice
# → "scissors"
shuffle
shuffle(xs: list): shuffled copy
(1..6).shuffle
# → [2, 1, 3, 4, 5]
See also: choice
roll
roll(dice?: str): roll dice like “3d6+2”, “d20”, “4d6kh3” (keep highest 3) or “2d20kl1”; returns {total, rolls}. Default 1d6
roll("3d6+2")
# → {total: 11, rolls: [2, 1, 6]}
roll("d%").total
# → 97
birthday_paradox
birthday_paradox(people: int, days?: int): the chance at least two of n people share a birthday (or any of days equally likely values)
birthday_paradox(23)
# → 0.507297
birthday_paradox(70)
# → 0.99916
birthday_paradox(10, 100)
# → 0.371843
odds
odds(p: num): a probability as “1 in N”, next to a familiar event about as likely
odds(1 / 1000)
# → "1 in 1,000: about as likely as flipping ten heads in a row (1 in 1,024)"
odds(0.5)
# → "1 in 2: about as likely as a coin landing heads (1 in 2)"
odds(2 ** -64)
# → "1 in 1.84 × 10^19: about as likely as a perfect March Madness bracket (1 in 9.20 × 10^18)"
See also: birthday_paradox
fortune
fortune(): a fortune cookie
fortune()
# → "Do not trust the last slice. It was left for a reason."
See also: eight_ball
eight_ball
eight_ball(question?: str): a magic 8-ball answer; the same question gets the same answer all day
eight_ball("is it Friday?")
# → "Most likely."
coin
coin(n?: int): heads or tails, or a list of n flips
coin()
# → "heads"
coin(3)
# → ["heads", "heads", "heads"]
See also: rand
yes_or_no
yes_or_no(question?: str): yes or no; leans yes on Fridays
yes_or_no()
# → "no"
See also: eight_ball
excuse
excuse(): why it doesn’t work
excuse()
# → "The bus left early. I saw it. It saw me."
See also: fortune
More examples
random
# roll a die
rand(1, 6)
# → 1
# float in a range
rand(1.0, 2.0)
# → 1.19859
# pick one
["rock", "paper", "scissors"].choice
# → "scissors"
# shuffle
(1..=5).shuffle
# → [2, 5, 1, 4, 3]
# a D&D stat: 4d6, keep the best 3
roll("4d6kh3")
# → {total: 11, rolls: [2, 1, 5, 4]}
# attack with advantage
roll("2d20kh1+5").total
# → 19
# best of five
coin(5)
# → ["heads", "heads", "heads", "tails", "tails"]
# a party of 23
birthday_paradox(23)
# → 0.507297
# how unlikely is the jackpot?
odds(1 / 292201338)
# → "1 in 292,201,338: about as likely as winning the Powerball jackpot (1 in 292,201,338)"
# ask the ball
eight_ball("should I get a dog?")
# → "Concentrate and ask again."
# why you're late
excuse()
# → "I was on hold for three hours."