random.h#

namespace ONNX_LIGHT_NAMESPACE
namespace onnx_backend_test

Functions

std::pair<uint64_t, uint64_t> NextUint64(uint64_t state)#

Computes the next SplitMix64 state and the corresponding random 64-bit value. The implementation matches the canonical SplitMix64 generator and is bit-identical to the Python reference in onnx_light.backend.random.

Parameters:

state – Current 64-bit state.

Returns:

std::pair{next_state, random_value}.

std::vector<double> Rand(const std::vector<int64_t> &shape, std::optional<uint64_t> seed = std::nullopt)#

Generates deterministic uniform random values in [0, 1) shaped as shape. An empty shape produces a single value (count 1).

Parameters:
  • shape – Output shape; dimensions must be non-negative.

  • seed – Optional 64-bit seed. std::nullopt selects the default seed (0).

Throws:

std::invalid_argument – when any dimension is negative.

Returns:

Flat row-major std::vector<double> of length prod(shape).

std::vector<int64_t> RandInt(int64_t low, int64_t high, const std::vector<int64_t> &shape, std::optional<uint64_t> seed = std::nullopt)#

Generates deterministic pseudo-random integers in the half-open interval [low, high) using rejection sampling on the SplitMix64 output to obtain an unbiased distribution.

Parameters:
  • low – Inclusive lower bound.

  • high – Exclusive upper bound. Must be strictly greater than low.

  • shape – Output shape; dimensions must be non-negative.

  • seed – Optional 64-bit seed. std::nullopt selects the default seed.

Throws:

std::invalid_argument – when high <= low or any dimension is negative.

Returns:

Flat row-major std::vector<int64_t> of length prod(shape).

std::vector<double> Randn(const std::vector<int64_t> &shape, std::optional<uint64_t> seed = std::nullopt)#

Generates deterministic pseudo-random values with an approximate normal distribution using the Irwin-Hall approximation (sum of 12 uniform values minus 6).

Parameters:
  • shape – Output shape; dimensions must be non-negative.

  • seed – Optional 64-bit seed. std::nullopt selects the default seed.

Throws:

std::invalid_argument – when any dimension is negative.

Returns:

Flat row-major std::vector<double> of length prod(shape).

Tensor RandBool(const std::vector<int64_t> &shape, std::optional<uint64_t> seed = std::nullopt)#

Generates a deterministic BOOL Tensor of the requested shape by drawing approximately-normal values from :cpp:func:Randn and thresholding at 0. Mirrors the upstream ONNX test pattern (np.random.randn(...) > 0).astype(bool) used by onnx.backend.test.case.node cases (e.g. And/Or/Xor).

Parameters:
  • shape – Output tensor shape; dimensions must be non-negative.

  • seed – Optional 64-bit seed. std::nullopt selects the default seed.

Throws:

std::invalid_argument – when any dimension is negative.

Returns:

A Tensor with data_type == BOOL whose data is a flat row-major buffer of one byte per element (0 or 1).

Variables

constexpr uint64_t kDefaultSeed = 0#

Default seed used when no explicit seed is provided.