The most popular (and primitive) sources of random numbers
available on a standard UNIX system are put together with GMP random number generators
under the unified wrapper interface:
Partially implements the STL Container
interface. The lacking methods are size(), max_size(), and empty(),
as they do not make much sense on an apparently unlimited data stream.
Two iterators over the same UniformRNG object will supply different data elements,
since the state information is kept by the latter and is being changed by each data request
(that is, iterator::operator*.)
However, an UniformRNG object copied from another one will produce the same data sequence,
since it inherits the whole internal state information from the original object, and the generating
algorithms are deterministic.
Currently, the class UniformRNG is defined for the following data types. While
the data access methods are identical (as prescribed by the Container interface,)
the constructors sometimes look differently:
UniformRNG< long > (unsigned long seed=0);
Generates integral numbers from the range [0, 231).
Uses nrand48, the stateless variant of the well-known lrand48() function.
UniformRNG< double > (unsigned long seed=0);
Generates floating-point numbers from the range [0,1).
Uses erand48, the stateless variant of the well-known drand48() function.
UniformRNG< Integer >
(unsigned long seed=0, unsigned long bitlength=48);
UniformRNG< Integer > (const Integer& seed, unsigned long bitlength=48);
Generates integral numbers from the range [0, 2bitlength).
Uses the GMP function mpz_urandomb.
UniformRNG< Rational >
(unsigned long seed=0, unsigned long bitlength=48);
UniformRNG< Rational >
(const Integer& seed, unsigned long bitlength=48);
Generates rational numbers from the range [0,1). Really only the numerators are random,
belonging to [0, 2bitlength); the denominators are always some powers of 2.
UniformRNG< Bitset >
(int max_elem, unsigned long seed=0);
Generates sets of integers belonging to the range [0, max_elem).
Effectively, it works just the same as UniformRNG< Integer >, but masquerades
the produced values in other way.
Generates floating-point numbers normally distributed in (-1,1).
The algorithm is taken from Donald E. Knuth, The Art of Computer Programming, vol. II, p117.
The number source is UniformRNG< double >.
There are two alias pseudo-container classes
randomly choosing elements from a given container.
Both implement the STL Container
interface. Repetitive data access operations will yield different selections!
Either a reference to a container whose lifetime is not shorter than that of the subset/permutation object,
or a "bare" container type for a temporary object.
See also the detailed discussion.
This is a convenience function, which allows to embed a temporary object into an expression
without writing down its exact type.
The result is identical to a direct call to the constructor of the corresponding class with
the same arguments.
Please note that this and similar convenience functions always create an object parameterized with references
to the input data (containers.) Sometimes, especially in a function return statement, you will need a reference-less variant;
then you have to use the constructor.
The template parameter inherits the const attribute from the corresponding function argument.
A pseudo-container with a const reference to the source data is in its turn immutable.
Choose k elements randomly. Each possible k-subset has equal probability
to appear. The chosen elements preserve their order in the original container c.
Visit the elements in a random order. Each possible permutation has equal probability
to appear. Container must be of bidirectional or random-access category.