This implementation is specialized for both 32-bit and 64-bit architectures. The generators differ sligthly in the initialization and generation phase so they produce two completly different sequences.
The generator gives back random numbers of serveral types. To get a random number from a range of a floating point type you can use one form of the operator()
or the real()
member function. If you want to get random number from the {0, 1, ..., n-1} integer range use the operator
[] or the integer()
method. And to get random number from the whole range of an integer type you can use the argumentless integer()
or uinteger()
functions. After all you can get random bool with equal chance of true and false or given probability of true result with the boolean()
member functions.
// The commented code is identical to the other double a = rnd(); // [0.0, 1.0) // double a = rnd.real(); // [0.0, 1.0) double b = rnd(100.0); // [0.0, 100.0) // double b = rnd.real(100.0); // [0.0, 100.0) double c = rnd(1.0, 2.0); // [1.0, 2.0) // double c = rnd.real(1.0, 2.0); // [1.0, 2.0) int d = rnd[100000]; // 0..99999 // int d = rnd.integer(100000); // 0..99999 int e = rnd[6] + 1; // 1..6 // int e = rnd.integer(1, 1 + 6); // 1..6 int b = rnd.uinteger<int>(); // 0 .. 2^31 - 1 int c = rnd.integer<int>(); // - 2^31 .. 2^31 - 1 bool g = rnd.boolean(); // P(g = true) = 0.5 bool h = rnd.boolean(0.8); // P(h = true) = 0.8
The lemon provides a global instance of the random number generator which name is rnd. Usually it is a good programming convenience to use this global generator to get random numbers.
#include <lemon/random.h>
Public Member Functions | |
Random () | |
Constructor. | |
template<typename Number > | |
Random (Number seed) | |
Constructor. | |
template<typename Iterator > | |
Random (Iterator begin, Iterator end) | |
Constructor. | |
Random (const Random &other) | |
Copy constructor. | |
Random & | operator= (const Random &other) |
Assign operator. | |
template<typename Number > | |
void | seed (Number seed) |
Seeding random sequence. | |
template<typename Iterator > | |
void | seed (Iterator begin, Iterator end) |
Seeding random sequence. | |
template<typename Number > | |
Number | real () |
Returns a random real number from the range [0, 1). | |
template<typename Number > | |
Number | real (Number b) |
Returns a random real number the range [0, b). | |
template<typename Number > | |
Number | real (Number a, Number b) |
Returns a random real number from the range [a, b). | |
double | operator() () |
Returns a random real number from the range [0, 1). | |
template<typename Number > | |
Number | operator() (Number b) |
Returns a random real number from the range [0, b). | |
template<typename Number > | |
Number | operator() (Number a, Number b) |
Returns a random real number from the range [a, b). | |
template<typename Number > | |
Number | integer (Number b) |
Returns a random integer from a range. | |
template<typename Number > | |
Number | integer (Number a, Number b) |
Returns a random integer from a range. | |
template<typename Number > | |
Number | operator[] (Number b) |
Returns a random integer from a range. | |
template<typename Number > | |
Number | uinteger () |
Returns a random non-negative integer. | |
template<typename Number > | |
Number | integer () |
Returns a random integer. | |
bool | boolean () |
Returns a random bool. | |
Nonuniform distributions | |
bool | boolean (double p) |
Returns a random bool. | |
double | gauss () |
Standard Gauss distribution. | |
double | gauss (double std_dev) |
Gauss distribution with given standard deviation and mean 0. | |
double | gauss (double mean, double std_dev) |
Gauss distribution with given mean and standard deviation. | |
double | exponential (double lambda=1.0) |
Exponential distribution with given mean. | |
double | gamma (int k) |
double | gamma (double k, double theta=1.0) |
Gamma distribution with given shape and scale parameter. | |
Two dimensional distributions | |
dim2::Point< double > | disc () |
Uniform distribution on the full unit circle. | |
dim2::Point< double > | gauss2 () |
A kind of two dimensional Gauss distribution. | |
dim2::Point< double > | exponential2 () |
A kind of two dimensional exponential distribution. |
Random | ( | ) | [inline] |
Constructor with constant seeding.
Random | ( | Number | seed | ) | [inline] |
Constructor with seed. The current number type will be converted to the architecture word type.
Random | ( | Iterator | begin, | |
Iterator | end | |||
) | [inline] |
Constructor with array seeding. The given range should contain any number type and the numbers will be converted to the architecture word type.
Copy constructor. The generated sequence will be identical to the other sequence. It can be used to save the current state of the generator and later use it to generate the same sequence.
Assign operator. The generated sequence will be identical to the other sequence. It can be used to save the current state of the generator and later use it to generate the same sequence.
void seed | ( | Number | seed | ) | [inline] |
Seeding the random sequence. The current number type will be converted to the architecture word type.
void seed | ( | Iterator | begin, | |
Iterator | end | |||
) | [inline] |
Seeding the random sequence. The given range should contain any number type and the numbers will be converted to the architecture word type.
Number real | ( | ) | [inline] |
It returns a random real number from the range [0, 1). The default Number type is double.
Number real | ( | Number | b | ) | [inline] |
It returns a random real number from the range [0, b).
Number real | ( | Number | a, | |
Number | b | |||
) | [inline] |
It returns a random real number from the range [a, b).
double operator() | ( | ) | [inline] |
It returns a random double from the range [0, 1).
Number operator() | ( | Number | b | ) | [inline] |
It returns a random real number from the range [0, b).
Number operator() | ( | Number | a, | |
Number | b | |||
) | [inline] |
It returns a random real number from the range [a, b).
Number integer | ( | Number | b | ) | [inline] |
It returns a random integer from the range {0, 1, ..., b - 1}.
Number integer | ( | Number | a, | |
Number | b | |||
) | [inline] |
It returns a random integer from the range {a, a + 1, ..., b - 1}.
Number operator[] | ( | Number | b | ) | [inline] |
It returns a random integer from the range {0, 1, ..., b - 1}.
Number uinteger | ( | ) | [inline] |
It returns a random non-negative integer uniformly from the whole range of the current Number
type. The default result type of this function is unsigned int.
Number integer | ( | ) | [inline] |
It returns a random integer uniformly from the whole range of the current Number
type. The default result type of this function is int.
bool boolean | ( | ) | [inline] |
It returns a random bool. The generator holds a buffer for random bits. Every time when it become empty the generator makes a new random word and fill the buffer up.
bool boolean | ( | double | p | ) | [inline] |
It returns a random bool with given probability of true result
double gauss | ( | ) | [inline] |
Standard Gauss distribution.
double gauss | ( | double | std_dev | ) | [inline] |
double gauss | ( | double | mean, | |
double | std_dev | |||
) | [inline] |
double exponential | ( | double | lambda = 1.0 |
) | [inline] |
This function generates an exponential distribution random number with mean 1/lambda
.
double gamma | ( | double | k, | |
double | theta = 1.0 | |||
) | [inline] |
This function generates a gamma distribution random number.
k | shape parameter (k>0 ) | |
theta | scale parameter |
dim2::Point<double> gauss2 | ( | ) | [inline] |
This function provides a turning symmetric two-dimensional distribution. Both coordinates are of standard normal distribution, but they are not independent.
dim2::Point<double> exponential2 | ( | ) | [inline] |
This function provides a turning symmetric two-dimensional distribution. The x-coordinate is of conditionally exponential distribution with the condition that x is positive and y=0. If x is negative and y=0 then, -x is of exponential distribution. The same is true for the y-coordinate.