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

Random Variates

The data type random_variate is a random number generator that generates ints according to an arbitrary static weight function.

The generation process is governed by an array<int> w. Let [l..r] be the index range of w and let W = w[l]+w[l+1]+...+w[r] be the total weight. Then any integer i between l and h is generated with probability w[i]/W. The weight function w must be non-negative and W must be non-zero.

Remark: The data type Random Variate can only generate ints.  

Example

The following program shows how to use random_variate.
#include <LEDA/core/random_variate.h>
#include <LEDA/core/array.h>

int main()
{
  leda::array<int> w(3);      //weight function
  w[0]=1;w[1]=2;w[2]=3;

  leda::random_variate R(w);  //random_variate
  int i=R.generate();   //random number
  std::cout << i << std::endl;

  return 0;
}

The program generates a very simple weight function w. Then it defines a random_variate R with weight function w. Finally it generates a random integer i using R. i has value 0 with probability 1/6, value 1 with probability 2/6, and 3 with probability 3/6.

See also:

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

Random Sources: a uniform random number generator

One Dimensional Arrays


Manual Page Random Variates




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