random module
Seedable, non-cryptographic RNG. Don't use this for tokens or password resets — use crypto for those. Good for shuffling, sampling, and tests with reproducible randomness.
import "random" as random;
random.seed(42); // reproducible
print(random.float()); // 0.0 ≤ x < 1.0
print(random.int(1, 6)); // dice roll, 1..6 inclusive
print(random.choice(["a","b","c"])); // pick one
print(random.shuffle([1,2,3,4,5])); // shuffled copy
API
| Function | Description |
|---|---|
random.seed(n) | Seed the RNG. Same seed → same sequence. |
random.next() → number | Uniform [0.0, 1.0). (Same as random.float().) |
random.float() → number | Alias for random.next(). |
random.bool() → bool | True / false with equal probability. |
random.probability(p) → bool | True with probability p (0.0–1.0). |
random.int(min, max) → number | Integer in [min, max] (inclusive both sides). |
random.range(lo, hi) → number | Uniform float in [lo, hi). |
random.bytes(n) → string | n random bytes (still not cryptographic). |
random.choice(xs) → element | Pick one element uniformly at random. |
random.shuffle(xs) → list | Fisher-Yates shuffle (new list). |
random.sample(xs, k) → list | k distinct elements without replacement. |