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]
| Function | Returns | Description |
|---|---|---|
randint(a, b) | Int | Random int N where a <= N <= b (inclusive) |
randrange(start, stop) | Int | Random int N where start <= N < stop |
seed(n) | None | Set 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