cheatah
Module

random

Pseudo-random numbers from a seedable Mersenne Twister (std::mt19937_64); gauss gives normal deviates for Monte Carlo. (For cryptographic randomness use os.urandom / ed25519, not this module.)

The engine is per-thread: concurrent draws from thread.spawned workers never race, and each thread's stream is independent. A thread self-seeds from std::random_device on first use; seed(s) seeds the calling thread's engine only — a worker that wants a reproducible stream calls seed itself.

import random

random.seed(42)          # reproducible stream
x = random.random()      # double in [0, 1)
d = random.randint(1, 6) # dice roll
pick = random.choice([1, 2, 3, 4, 5])

Functions

  • seed(s) — seed the calling thread's engine, making its stream reproducible.

  • random() — uniform double in [0, 1).

  • uniform(a, b) — uniform double in [a, b].

  • randint(a, b) — uniform integer in [a, b] (inclusive).

  • gauss(mu, sigma) — normal (Gaussian) deviate.

  • choice(seq) — a random element of a random-access sequence (list/array).

Per-function docs (parameters, runtime complexity, heap behavior) are in random.hpp. Tested in ../tests/random_test.cpp; ASan + Valgrind clean via the QA gate (security/run-valgrind.sh).

Functions

fn void seed(unsigned long long s) source#

Seed the calling thread's engine, making its stream reproducible.

Reseeds THIS thread's Mersenne Twister; all random/uniform/randint/gauss/choice calls on the same thread draw from it, so two runs seeded with the same value produce identical sequences. Until seed is called a thread's engine is seeded non-deterministically from std::random_device — a thread.spawned worker that wants reproducibility calls seed itself (the main thread's seed does not reach it).

Parameters
s

the seed.

Complexity

O(1) time.

Allocation

none.

Concurrency

seeds the calling thread's engine only; other threads' streams are unaffected.

Compile-run testRandomCompileRun.Seed
Performanceone-time initialization — not a hot path
fn double random() source#

Uniform random double.

Draws from the calling thread's engine with a uniform real distribution over the half-open unit interval, so 0.0 can occur but 1.0 cannot.

Returns

a value in [0, 1).

Complexity

O(1) time.

Allocation

none.

Concurrency

thread-safe — draws from the per-thread (thread_local) engine, so concurrent draws never race.

Compile-run testRandomCompileRun.Random
Performance3.81 ns/call in cheatah · 246 ns/call in NumPy 1.26.4 · ≈64.1× faster
fn double uniform(double a, double b) source#

Uniform random double in a range.

Scales a uniform real distribution to span the given bounds; the caller is expected to pass ab (the bounds are not reordered or validated).

Parameters
a, b

the bounds.

Returns

a value in [a, b) — the upper bound is excluded, like random().

Complexity

O(1) time.

Allocation

none.

Concurrency

thread-safe — draws from the per-thread (thread_local) engine, so concurrent draws never race.

Compile-run testRandomCompileRun.Uniform
Performance4.00 ns/call in cheatah · 900 ns/call in NumPy 1.26.4 · ≈228.8× faster
fn long long randint(long long a, long long b) source#

Uniform random integer.

Returns each integer in the closed range with equal probability; both a and b are attainable, and a == b always yields that value.

Parameters
a, b

inclusive bounds.

Returns

an integer in [a, b].

Complexity

O(1) time.

Allocation

none.

Concurrency

thread-safe — draws from the per-thread (thread_local) engine, so concurrent draws never race.

Compile-run testRandomCompileRun.Randint
Performance5.47 ns/call in cheatah · 1159 ns/call in NumPy 1.26.4 · ≈207.6× faster
fn double gauss(double mu, double sigma) source#

Normal (Gaussian) deviate.

Samples the normal distribution N(mu, sigma²) from the calling thread's engine; the result is unbounded and can fall on either side of the mean.

Parameters
mu

mean.

sigma

standard deviation.

Returns

a normal sample.

Complexity

O(1) time.

Allocation

none.

Concurrency

thread-safe — draws from the per-thread (thread_local) engine, so concurrent draws never race.

Compile-run testRandomCompileRun.Gauss
Performance28.82 ns/call in cheatah · 382 ns/call in NumPy 1.26.4 · ≈13.3× faster
fn std::ranges::range_value_t< R > choice(const R &seq) source#

Random element of a random-access sequence (list/array).

Picks a uniformly random index in [0, size) via randint and returns a copy of that element.

Warning

The sequence must be non-empty — an empty seq passes an inverted range to randint, which is undefined.

Parameters
seq

the sequence to pick from (must be non-empty).

Returns

a copy of a uniformly chosen element.

Complexity

O(1) time.

Allocation

copies the chosen element — none unless the element's copy itself allocates (e.g. str).

Concurrency

thread-safe — draws its index from the per-thread engine via randint.

Compile-run testRandomCompileRun.Choice
System testStdlibE2E.Random
Performance5.60 ns/call in cheatah · 4377 ns/call in NumPy 1.26.4 · ≈806.4× faster