nk
1.0.0
C++ Numerical Computing Runtime
Toggle main menu visibility
Loading...
Searching...
No Matches
rng.hpp
1
// ops/include/nk/ops/rng.hpp
2
//
3
// Value-producing random generators (rand / randn / randi / randperm). Every
4
// one draws from a caller-provided RngContext — the seedable session stream the
5
// Engine owns (engine.rng()); there is NO process-global RNG and NO mutex. A
6
// single session shares one reproducible stream (MATLAB `rng(seed)` semantics);
7
// two Engines are independent. RngContext models UniformRandomBitGenerator, so
8
// the std distributions here draw from it directly (`dist(rng)`).
9
//
10
// An L0.5 ops primitive (Value + RngContext, no Engine). The user-facing
11
// rand/randn/randi/randperm/rng *builtins* (the CallContext wrappers that parse
12
// shape/type args) live in bundle and call these with engine.rng().
13
14
#pragma once
15
16
#include <memory_resource>
17
#include <nk/value/span.hpp>
18
#include <nk/value/value.hpp>
19
20
#include <nk/ops/rng_context.hpp>
21
22
#include <cstdint>
23
24
namespace
nk::ops {
25
26
// ── Real-valued random (uniform / standard-normal) ───────────────────
27
// `pages == 0` → 2-D matrix. Output is DOUBLE.
28
Value rand(
RngContext
&rng,
size_t
rows,
size_t
cols = 1,
29
size_t
pages = 0, std::pmr::memory_resource *mr =
nullptr
);
30
Value randn(
RngContext
&rng,
size_t
rows,
size_t
cols = 1,
31
size_t
pages = 0, std::pmr::memory_resource *mr =
nullptr
);
32
Value randND(
RngContext
&rng, Span<const size_t> dims,
33
std::pmr::memory_resource *mr =
nullptr
);
34
Value randnND(
RngContext
&rng, Span<const size_t> dims,
35
std::pmr::memory_resource *mr =
nullptr
);
36
37
// ── Integer random ───────────────────────────────────────────────────
38
Value randi(
RngContext
&rng, int64_t imax, std::pmr::memory_resource *mr =
nullptr
);
39
Value randi(
RngContext
&rng, int64_t imax,
size_t
rows,
size_t
cols,
40
size_t
pages = 0, std::pmr::memory_resource *mr =
nullptr
);
41
Value randi(
RngContext
&rng, int64_t imin, int64_t imax,
size_t
rows,
size_t
cols,
42
size_t
pages = 0, std::pmr::memory_resource *mr =
nullptr
);
43
44
// ── Permutations ─────────────────────────────────────────────────────
45
Value randperm(
RngContext
&rng,
size_t
n, std::pmr::memory_resource *mr =
nullptr
);
46
Value randperm(
RngContext
&rng,
size_t
n,
size_t
k,
47
std::pmr::memory_resource *mr =
nullptr
);
48
49
// rng() control (seed / shuffle / state save+restore) lives on RngContext
50
// itself — see rng_context.hpp.
51
52
}
// namespace nk::ops
nk::ops::RngContext
Definition
rng_context.hpp:29
src
ops
include
nk
ops
rng.hpp
Generated by
1.17.0