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
randa random decimal from 0 up to but not including 1seedmake the session’s random choices repeatablechoicea random item from the listshufflea copy of the list in random orderrollroll dice written as in tabletop games, like “3d6+2”, and return {total, rolls}birthday_paradoxthe chance that at least two of n people share a birthdayoddsa probability as “1 in N”, with a familiar event that’s about as likely, to give a feel for itfortunea random fortune-cookie sayingeight_ballan answer from a Magic 8-Ball toycoin“heads” or “tails”, or a list of n flipsyes_or_noa random “yes” or “no”, a little more likely to be yes on Fridaysexcusea random excuse for why the code doesn’t work
Functions
Section titled “Functions”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
1rand()0.855182rand(1, 6)6seed(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
1seed(7); rand(1, 100)78See also rand
choice
Section titled “choice”choice(xs: list) -> any
a random item from the list
1["rock", "paper", "scissors"].choice"rock"shuffle
Section titled “shuffle”shuffle(xs: list) -> list
a copy of the list in random order
1(1..6).shuffle[5, 4, 1, 3, 2]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
1roll("3d6+2"){total: 18, rolls: [5, 6, 5]}2roll("d%").total81birthday_paradox
Section titled “birthday_paradox”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
1birthday_paradox(23)0.5072972birthday_paradox(70)0.999163birthday_paradox(10, 100)0.371843odds(p: num) -> str
a probability as “1 in N”, with a familiar event that’s about as likely, to give a feel for it
1odds(1 / 1000)"1 in 1,000: about as likely as flipping ten heads in a row (1 in 1,024)"2odds(0.5)"1 in 2: about as likely as a coin landing heads (1 in 2)"3odds(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
Section titled “fortune”fortune() -> str
a random fortune-cookie saying
1fortune()"You will be asked to help someone move. Have an excuse ready."See also eight_ball
eight_ball
Section titled “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
1eight_ball("is it Friday?")"Signs point to yes."coin(n?: int) -> str|list
“heads” or “tails”, or a list of n flips
1coin()"tails"2coin(3)["heads", "tails", "tails"]See also rand
yes_or_no
Section titled “yes_or_no”yes_or_no(question?: str) -> str
a random “yes” or “no”, a little more likely to be yes on Fridays
1yes_or_no()"yes"See also eight_ball
excuse
Section titled “excuse”excuse() -> str
a random excuse for why the code doesn’t work
1excuse()"I'm not late, everyone else is early."See also fortune
More examples
Section titled “More examples”random
Section titled “random”1# roll a die2rand(1, 6)43# a decimal between 1 and 24rand(1.0, 2.0)1.034965# pick one6["rock", "paper", "scissors"].choice"scissors"7# shuffle8(1..=5).shuffle[2, 4, 1, 5, 3]9# a D&D stat: 4d6, keep the best 310roll("4d6kh3"){total: 13, rolls: [6, 4, 3, 2]}11# roll 2d20, keep the higher, add 512roll("2d20kh1+5").total2313# five coin flips14coin(5)["tails", "tails", "tails", "tails", "heads"]15# the chance two of 23 people share a birthday16birthday_paradox(23)0.50729717# how unlikely is the jackpot?18odds(1 / 292201338)"1 in 292,201,338: about as likely as winning the Powerball jackpot (1 in 292,201,338)"19# ask the ball20eight_ball("should I get a dog?")"Outlook good."21# why you're late22excuse()"My socks didn't match and I couldn't go on."Example results generated on 2026-11-04. Ones using now, today or randomness will differ when you run them.