random
Random integers, floats, booleans, and picks from an array.
introduce random| Function | Signature | Returns |
|---|---|---|
rand_int | rand_int(min, max) | int in [min, max] |
rand_float | rand_float() | float in [0.0, 1.0) |
rand_bool | rand_bool() | bool |
choice | choice(arr) | one element |
seed | seed(n) | None |
Not cryptographically secure. Do not use this for tokens, passwords, session IDs, or keys.
#rand_int(min, max)
A random integer between min and max, both inclusive.
println(random.rand_int(1, 6)) // 4
println(random.rand_int(0, 1)) // 0#rand_float()
A random float in [0.0, 1.0) — zero is possible, one is not.
println(random.rand_float()) // 0.5488135039273248Scale it for any other range:
fun uniform(lo <float>, hi <float>) { lo + random.rand_float() * (hi - lo) }#rand_bool()
A coin flip. Equivalent to rand_int(0, 1) == 1.
println(random.rand_bool()) // True#choice(arr)
One element picked uniformly at random.
println(random.choice(["red", "green", "blue"])) // greenAn empty array is an error — len(arr) - 1 is -1, so the index is out of
range. Guard it if the array might be empty.
#seed(n)
Seed the generator. The same seed replays the same sequence, which is what makes a test with random input reproducible.
introduce random
random.seed(42)
println(random.rand_int(1, 100)) // 26 — the same value on every runWithout a seed call the generator starts from an unpredictable value, so each
run differs.
#Worked example
introduce random
introduce array
// Shuffle by decorating with random keys and sorting on them.
fun shuffled(arr <array>) {
keyed := array.map(arr, fun(x) { (random.rand_int(0, 1000000), x) })
array.map(array.sort_by(keyed, fun(p) { p[0] }), fun(p) { p[1] })
}
random.seed(7)
println(shuffled([1, 2, 3, 4, 5])) // [2, 5, 3, 4, 1] (same every run, given the seed)
// Weighted coin: True about 30% of the time.
fun chance(p <float>) { random.rand_float() < p }
println(chance(0.3))See also: math for numeric helpers, hash for digests (also not a random source).