Tanguv0.35.0
Math, SIMD & media7 exports
Raw Markdown

/rng

Pseudo-random number generation for bounded integers, floats, booleans, and collection sampling.

(use /rng)

Functions & symbols#

choose#

function
(rng/choose coll) -> Any

Uniformly selects a single element from a collection, or returns `()` if empty.

ParameterTypeOptionalDescription
collAnyrequiredSequence, Range, Slab, String, Set, or Map
Returns:Any
(rng/choose [:a :b :c])
(rng/choose 1..=6)

random-bool#

function
(rng/random-bool) -> Bool

Generates a random Bool with fair 50/50 odds or Bernoulli probability `p` in `0.0..=1.0`.

ParameterTypeOptionalDescription
pFloatrequiredOptional Bernoulli probability in `0.0..=1.0` (defaults to `0.5`)
Returns:Any
(rng/random-bool)
(rng/random-bool 0.25)

random-float#

function
(rng/random-float) -> Float

Generates a uniform random Float in `[0.0, 1.0)`, `[0.0, end)`, `[start, end)`, or a Range.

ParameterTypeOptionalDescription
end-or-rangeAnyrequiredOptional upper bound `end > 0.0` or float `Range` (e.g. `-1.0..1.0`)
endFloatrequiredOptional exclusive upper bound when `start` is provided
Returns:Any
(rng/random-float)
(rng/random-float 10.0)
(rng/random-float -1.0..1.0)

random-int#

function
(rng/random-int end) -> Int

Generates a uniform random Int in `[0, end)`, `[start, end)`, or from an integer Range.

ParameterTypeOptionalDescription
end-or-rangeAnyrequiredExclusive upper bound `end > 0` or integer `Range` (e.g. `1..=6`, `0..=100|5`)
endIntrequiredOptional exclusive upper bound when `start` is provided
Returns:Any
(rng/random-int 100)
(rng/random-int 10 20)
(rng/random-int 1..=6)

sample#

function
(rng/sample coll k) -> Any

Samples `k` distinct elements without replacement in random order, clamping `k`.

ParameterTypeOptionalDescription
collAnyrequiredSequence, Range, Slab, String, Set, or Map
kIntrequiredNumber of distinct elements to sample (clamped to `0..(count-of coll)`)
Returns:Any
(rng/sample [1 2 3 4 5] 3)
(rng/sample 1..=52 5)

shuffle#

function
(rng/shuffle coll) -> Any

Returns a new collection with its elements shuffled in uniformly random order.

ParameterTypeOptionalDescription
collAnyrequiredSequence, Range, Slab, String, Set, or Map
Returns:Any
(rng/shuffle [1 2 3 4 5])

shuffle<-#

function
(rng/shuffle<- #mut coll) -> Any

Shuffles a mutable collection in place using Fisher-Yates and returns the mutated collection.

ParameterTypeOptionalDescription
coll#mut AnyrequiredMutable Array, Int-Array, Float-Array, Dec-Array, Slab, or Map
Returns:Any
(let #mut deck [1 2 3 4]) (rng/shuffle<- deck)