stats/rng
rng-make
(rng-make [seed :int stream :int] :int)
create a new PCG32 stream seeded with seed.
| seed | :int seed value; 0 uses a default fixed seed (PCG default) | |
| stream | :int stream discriminator; use different values for independent streams |
:int opaque rng handle. Free with rng-free.
(let [r (rng-make 42 1)]
(rng-uniform r)) ; => deterministic float in [0,1)
Since: ST0
rng-clone
(rng-clone [r :int] :int)
create an independent copy of rng r at the same state.
| r | :int source rng handle |
:int new rng handle at the same state. Free with rng-free.
(let [a (rng-make 1 1)
b (rng-clone a)]
(= (rng-uniform a) (rng-uniform b))) ; => true
Since: ST0
rng-free
(rng-free [r :int] :void)
free a rng handle.
| r | :int rng handle |
:void
(rng-free (rng-make 1 1))
Since: ST0
rng-uint32
(rng-uint32 [r :int] :int)
draw a uniform 32-bit integer from [0, 2^32).
| r | :int rng handle |
:int value in [0, 2^32).
(rng-uint32 (rng-make 1 1)) ; => some :int
Since: ST0
rng-uint64
(rng-uint64 [r :int] :int)
draw a uniform 64-bit integer.
| r | :int rng handle |
:int 64-bit random value (sign bit may be set).
Since: ST0
rng-uniform
(rng-uniform [r :int] :float)
draw a uniform float in [0, 1).
| r | :int rng handle |
:float in [0, 1).
(rng-uniform (rng-make 42 1))
Since: ST0
rng-uniform-range
(rng-uniform-range [r :int lo :float hi :float] :float)
draw a uniform float in [lo, hi).
| r | :int rng handle | |
| lo | :float lower bound (inclusive) | |
| hi | :float upper bound (exclusive) |
:float in [lo, hi).
(rng-uniform-range r -1.0 1.0)
Since: ST0
rng-int-range
(rng-int-range [r :int lo :int hi :int] :int)
draw a uniform integer in [lo, hi).
| r | :int rng handle | |
| lo | :int lower bound (inclusive) | |
| hi | :int upper bound (exclusive) |
:int in [lo, hi).
(rng-int-range r 0 6) ; => 0..5
Since: ST0
rng-shuffle!
(rng-shuffle! [r :int indices :int] :void)
in-place Fisher-Yates shuffle of a cons list of :int indices.
| r | :int rng handle | |
| indices | cons list of :int (modified in-place) |
:void
(let [lst (cons 0 (cons 1 (cons 2 0)))]
(rng-shuffle! r lst))
Since: ST0
rng-shuffled
(rng-shuffled [r :int indices :int] :int)
return a new shuffled copy of a cons list of :int.
| r | :int rng handle | |
| indices | cons list of :int |
New cons list of :int in shuffled order. Caller owns the new list.
Since: ST0
rng-sample-indices
(rng-sample-indices [r :int n :int k :int with-replacement :int] :int)
sample k indices from [0, n) with or without replacement.
| r | :int rng handle | |
| n | :int population size | |
| k | :int sample size | |
| with-replacement | :int 0 = without replacement, 1 = with replacement |
cons list of :int (k indices in [0, n)). Caller frees.
(rng-sample-indices r 100 10 0) ; => 10 distinct indices
Since: ST0
rng-sample
(rng-sample [r :int col :int n :int with-replacement :int] :int)
sample n rows from a float64 column, with or without replacement.
| r | :int rng handle | |
| col | :int tur-frame float64 column handle | |
| n | :int number of rows to sample | |
| with-replacement | :int 0 = without, 1 = with |
:int new float64 column of length n. Caller frees.
Since: ST0