No matching definitions.

tur/random

stdlib/random.tur

Random capability implementation using the platform RNG.

Since: Phase 16

defn

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

defn

Real-Random-free

(Real-Random-free [rng])

release a Random capability created by Real-Random.

rngthe Random pointer returned by Real-Random.

Since: Phase 16

defn

Seeded-Random

(Seeded-Random [seed])

create a deterministic Random capability from a fixed seed.

seedunsigned 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

defn

Seeded-Random-free

(Seeded-Random-free [rng])

release a Random capability created by Seeded-Random.

rngthe Random pointer returned by Seeded-Random.

Since: Phase 16

defn

rand-int

(rand-int [min max])

return a random integer in the inclusive range [min, max].

minthe minimum value (inclusive).
maxthe 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

defn

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

defn

rand-die

(rand-die [sides])

roll an n-sided die, returning a value in [1, sides].

sidesthe number of faces on the die.

A random integer in [1, sides].

(rand-die 20)  ; => 13  (D20 roll)

Since: Phase 16

defn

rand-bool

(rand-bool)

return a random boolean value.

1 (true) or 0 (false) with equal probability.

(rand-bool)  ; => 1

Since: Phase 16