Reference · General
random_int
signature
random_int(int, int) -> intWhat it does
An int from lo to hi, both ends included, different on each call. Like
random it is fast and predictable, so it is for dice, picks
and jitter, not for secrets. There is no seed.
Examples
let n = random_int(1, 6)print(type_of(n), n >= 1, n <= 6)let rolls = range(1000) | map((i) => random_int(1, 6))print(rolls | min, rolls | max, rolls | unique | sort)output
int true true1 6 [1, 2, 3, 4, 5, 6]Edge cases
- Both ends are included:
random_int(1, 6)can be 6. lo == higives that number; negative ends are fine.- Any int range works, however wide.
print(random_int(3, 3), random_int(-5, -5), random_int(0, 0))print(random_int(-1000000000000, 1000000000000) | type_of)output
3 -5 0intErrors
lo above hi is a runtime error, not an error value, and so is an end that is not an
int: a float is refused, convert it with int first.
this one fails
print(random_int(5, 1))output
line 1: `random_int` needs lo <= hi, got 5 and 1this one fails
print(random_int(1.5, 3))output
line 1: `random_int` expects an int as argument 1, got floatChaining
let xs = ["ann", "bob", "cy"]print(type_of(xs[random_int(0, len(xs) - 1)]))print(range(3) | map((i) => random_int(10, 20)) | all((n) => n >= 10 && n <= 20))output
strtrueSee also
Thread
Thread 0 posts
On random_int(): questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.
Edit this page on GitHub · every example's output was made by running it (tools/refcheck.mjs) · questions on Discord or r/quillo