Algorithmic Solutions > LEDA > LEDA Guide > Simple, Basic, and Advanced Data Types > Simple Data Types > Random Sources

Random Sources

The data type random_source is a random number generator (based on the function random() from Berkeley UNIX). It can be used to generate uniformly distributed random bits, characters, ints and doubles.

It can be in either of two modes:

  • In bit mode: it generates a random bit string of some length p ( 1 < = p < = 31)
  • In integer mode: it generates a random integer in some range [low..high] ( low < = high < low + 231).

Remark: The data type random_source is only pseudorandom. The seed depends on the internal clock.

Example

The following program generates a random integer between 0 and 231 and outputs it.
#include <LEDA/core/random_source.h>

int main()
{
  leda::random_source S;
  int ri;
  S >> ri;
  std::cout << ri << std::endl;

  return 0;
}

Tip

Extract characters and boolean values only from sources whose range spans powers of two, otherwise the distribution is not uniform.

See also:

Random Variates: non-uniform random number generator with static weight function

Dynamic Random Variate: non-uniform random number generator with dynamic weight function


Manual Page Random Sources




Please send any suggestions, comments or questions to leda@algorithmic-solutions.com
© Copyright 2001-2003, Algorithmic Solutions Software GmbH