Pseudorandom samples, distribution helpers, shuffling, and selection.
CRandom
Methods
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.
Copies the random engine’s current state. It does not update the separate seed field or copy the stored uniform-distribution state.
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.
void setSeed(uint64_t seed);
Restarts the engine from the supplied seed. The seed() accessor is not updated by this call.
Current limitation: this accessor is not kept in sync with constructor, setSeed(), or timeSeed() seeding.
Reseeds from cTicks() and returns the seed actually used. Save that return value if you need to reproduce the sequence later.
double uniform();
double uniform(double a, double b);
Samples [0,1), or the corresponding interval [a,b).
int64_t equilikely(int64_t a, int64_t b);
Samples an integer from the inclusive interval [a,b].
double exponential(double x);
Samples an exponential distribution with rate x.
double normal(double m, double s);
Samples with mean m and standard deviation s.
bool bernoulli(double p);
Returns true with probability p, which must be in [0,1].
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.
int64_t poisson(double m);
Samples a nonnegative integer with mean m, which must be positive.
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.
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.
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.
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.
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.