System & concurrency

CRandom

Pseudorandom samples, distribution helpers, shuffling, and selection.

C++23 mc/CRandom.h
#include <mc/CRandom.h>

Use an explicit seed for repeatable sampling. This generator is intended for simulation and randomized algorithms, not security tokens.

Jump to a declaration · 19

CRandom

class CRandom

Methods

CRandom

explicit CRandom(uint64_t seed);
CRandom();
CRandom(CBuffer& b);
explicit CRandom(const CRandom& r);

Creates a generator from an explicit seed, the current tick counter, a saved buffer, or another generator’s engine state. An explicit seed is the clearest way to reproduce a sequence; see seed() and store() for the current stored-seed limitation.

operator=

CRandom& operator=(const CRandom& r);

Copies the random engine’s current state. It does not update the separate seed field or copy the stored uniform-distribution state.

store

void store(CBuffer& b) const;

Writes the seed field and serialized engine/distribution states into a buffer. Current limitation: ordinary construction and reseeding do not initialize or update the stored seed field, so this serialization path should not be relied on until corrected.

setSeed

void setSeed(uint64_t seed);

Restarts the engine from the supplied seed. The seed() accessor is not updated by this call.

seed

uint64_t seed() const;

Current limitation: this accessor is not kept in sync with constructor, setSeed(), or timeSeed() seeding.

timeSeed

uint64_t timeSeed();

Reseeds from cTicks() and returns the seed actually used. Save that return value if you need to reproduce the sequence later.

uniform

double uniform();
double uniform(double a, double b);

Samples [0,1), or the corresponding interval [a,b).

equilikely

int64_t equilikely(int64_t a, int64_t b);

Samples an integer from the inclusive interval [a,b].

exponential

double exponential(double x);

Samples an exponential distribution with rate x.

normal

double normal(double m, double s);

Samples with mean m and standard deviation s.

bernoulli

bool bernoulli(double p);

Returns true with probability p, which must be in [0,1].

binomial

int64_t binomial(int64_t n, double p);

Current limitation: the implementation’s result accumulator is uninitialized. This method is listed but should not be relied on until corrected.

poisson

int64_t poisson(double m);

Samples a nonnegative integer with mean m, which must be positive.

expSelect

uint64_t expSelect(uint64_t max, double s);

Selects an integer in the inclusive range [0,max] with an exponential bias toward smaller values. s must be positive; larger values produce a stronger bias.

randomSequence

template<class I, CItems S = CHashSet<double>> void randomSequence(int64_t a, int64_t b, CVector<I>& v, size_t samples = 0);

Adds integers from the inclusive range [a,b] and shuffles the entire destination, including existing elements. Zero samples or a count at least b-a adds the full range; partial sampling can produce fewer entries than requested because duplicate draws are discarded.

randomSet

template<CItems S> void randomSet(int64_t a, int64_t b, S& s, size_t samples);

Inserts distinct integers from the inclusive range [a,b]. Counts at least b-a insert the whole range; smaller counts request that many new entries, so the destination must leave enough unused values for sampling to finish.

shuffle

template<CItems S> void shuffle(S& v);

Randomly permutes the existing elements in place using this generator. The collection must support mutable random-access iteration.

choose

template<CItems S> decltype(auto) choose(S& s);
template<CItems S> decltype(auto) choose(const S& s);

Returns an element from a nonempty indexable collection, preserving the collection’s reference return type. A borrowed result remains subject to the collection’s normal invalidation rules.