Standard Library

Random

The random module provides pseudorandom integer generation. For cryptographically secure randomness, use the crypto module instead.

from random import randint, seed

seed(42)                    # reproducible sequence
let roll = randint(1, 6)    # random integer in [1, 6]

Functions

FunctionReturnsDescription
randint(a, b)IntRandom int N where a <= N <= b (inclusive)
randrange(start, stop)IntRandom int N where start <= N < stop
seed(n)NoneSet RNG seed for reproducible sequences
from random import randint, randrange, seed

# Reproducible results
seed(123)
let a = randint(1, 100)
let b = randrange(0, 10)    # 0..9

# Dice simulation
let die = randint(1, 6)
let coin = randint(0, 1)    # 0 = tails, 1 = heads
← Static HTTP Server
Bytes →