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
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).
s | the seed. |
O(1) time.
none.
seeds the calling thread's engine only; other threads' streams are unaffected.
RandomCompileRun.SeedStdlibE2E.Random SystemApps.MonteCarloUniform 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.
a value in [0, 1).
O(1) time.
none.
thread-safe — draws from the per-thread (thread_local) engine, so concurrent draws never race.
CheatahRandom.RandomInUnitIntervalRandomCompileRun.RandomStdlibE2E.Random SystemApps.MonteCarloUniform random double in a range.
Scales a uniform real distribution to span the given bounds; the caller is expected to pass a ≤ b (the bounds are not reordered or validated).
a, b | the bounds. |
a value in [a, b) — the upper bound is excluded, like random().
O(1) time.
none.
thread-safe — draws from the per-thread (thread_local) engine, so concurrent draws never race.
CheatahRandom.UniformInRangeRandomCompileRun.UniformStdlibE2E.Random SystemApps.MonteCarloUniform 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.
a, b | inclusive bounds. |
an integer in [a, b].
O(1) time.
none.
thread-safe — draws from the per-thread (thread_local) engine, so concurrent draws never race.
CheatahRandom.RandintInclusiveRangeRandomCompileRun.RandintStdlibE2E.Random SystemApps.MonteCarloNormal (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.
mu | mean. |
sigma | standard deviation. |
a normal sample.
O(1) time.
none.
thread-safe — draws from the per-thread (thread_local) engine, so concurrent draws never race.
RandomCompileRun.GaussStdlibE2E.Random SystemApps.MonteCarloRandom 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.
The sequence must be non-empty — an empty seq passes an inverted range to randint, which is undefined.
seq | the sequence to pick from (must be non-empty). |
a copy of a uniformly chosen element.
O(1) time.
copies the chosen element — none unless the element's copy itself allocates (e.g. str).
thread-safe — draws its index from the per-thread engine via randint.
CheatahRandom.ChoiceRandomCompileRun.ChoiceStdlibE2E.Random