numir.random
Simple mir-random
wrappers for convenience
-
Declaration
class
RNG
; -
Declaration
auto
generate
(V, size_t N)(Vvar
, size_t[N]length
...);general function for random slice generation with global RNG
-
Declaration
auto
normal
(E = double, size_t N)(size_t[N]length
...); -
Declaration
auto
uniform
(E = double, size_t N)(size_t[N]length
...);Examples
import mir.ndslice : all; import std.algorithm : sum; import mir.random.variable : BernoulliVariable; auto bs = BernoulliVariable!double(0.25).generate(100).sum; assert(0 < bs && bs < 50, "maybe fail"); // pre-defined random variables (normal and uniform) RNG.setSeed(1); auto r0 = normal(3, 4).slice; assert(r0.shape == [3, 4]); RNG.setSeed(0); auto r1 = normal(3, 4).slice; assert(r0 != r1); RNG.setSeed(0); auto r2 = normal(3, 4).slice; assert(r1 == r2); auto u = uniform(3, 4).slice; assert(u.shape == [3, 4]); assert(u.all!(a => (0 <= a && a < 1)));
-
Declaration
auto
permutation
(T...)(Tt
);generate a sequence as same as numir.core.arange but shuffled
Examples
import numir : arange; import mir.ndslice.sorting : sort; import numir.testing : approxEqual; import std.stdio; auto ps1 = permutation(100); auto ps2 = permutation(100); assert(ps1 != ps2, "maybe fail at 1%"); assert(ps1.sort() == arange(100)); auto ps3 = permutation(1, 10, 0.1); auto ps4 = permutation(1, 10, 0.1); assert(ps3 != ps4); assert(ps4.sort.approxEqual(arange(1, 10, 0.1)));