tur/random
Random capability implementation using the platform RNG.
Since: Phase 16
Real-Random
(Real-Random)
create a Random capability backed by libc rand().
A heap-allocated Random struct with: next_int -- returns a random integer in [min, max]. next_float -- returns a random integer in [0, 9999] (divide by 10000.0 to obtain a float in [0, 1)). The RNG is seeded with time(NULL) on first use. Free with Real-Random-free when done.
(let [rng (Real-Random)] ...)
Since: Phase 16
Real-Random-free
(Real-Random-free [rng])
release a Random capability created by Real-Random.
| rng | the Random pointer returned by Real-Random. |
Since: Phase 16
Seeded-Random
(Seeded-Random [seed])
create a deterministic Random capability from a fixed seed.
| seed | unsigned integer seed for srand; same seed produces the same sequence. |
A heap-allocated Random struct identical in interface to Real-Random but seeded immediately with the supplied value. Useful for reproducible tests. Free with Seeded-Random-free when done.
(let [rng (Seeded-Random 42)] ...)
Since: Phase 16
Seeded-Random-free
(Seeded-Random-free [rng])
release a Random capability created by Seeded-Random.
| rng | the Random pointer returned by Seeded-Random. |
Since: Phase 16
rand-int
(rand-int [min max])
return a random integer in the inclusive range [min, max].
| min | the minimum value (inclusive). | |
| max | the maximum value (inclusive). |
A random integer in [min, max]. The libc RNG is auto-seeded with time(NULL) on first call.
(rand-int 1 6) ; => 4 (simulate a die roll)
Since: Phase 16
rand-float
(rand-float)
return a random integer scaled to represent [0, 1).
An integer in [0, 9999]. Divide by 10000.0 to obtain a float in [0, 1). The libc RNG is auto-seeded with time(NULL) on first call.
(rand-float) ; => 7312 ; represents ~0.7312
Since: Phase 16
rand-die
(rand-die [sides])
roll an n-sided die, returning a value in [1, sides].
| sides | the number of faces on the die. |
A random integer in [1, sides].
(rand-die 20) ; => 13 (D20 roll)
Since: Phase 16
rand-bool
(rand-bool)
return a random boolean value.
1 (true) or 0 (false) with equal probability.
(rand-bool) ; => 1
Since: Phase 16