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

Dynamic Random Variates

The data type dynamic_random_variate is a random number generator that generates ints according to an arbitrary dynamic weight function. This data type is a dynamic version of Random Variates.

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. The weight function can be changed during the program.

Remark: Dynamic Random Variates can only generate ints.

Example

The following example shows how dynamic_random_variate can be used:
#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::dynamic_random_variate R(w);  //dynamic_random_variate

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

  R.set_weight(0,4);    //set weight w[0]=4

  int j=R.generate();   //second random number
  std::cout << j << std::endl;

  return 0;
}
The program generates a very simple weight function w and defines a dynamic_random_variate R with weight function w.

Then 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.

Finally the value w[0] is changed to 4 and another random integer j is generated. The value of j is 0 with probability 4/9, 2 with probability 2/9, and 3 with probability 3/9.

See also:

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

Random Sources: a uniform random number generator

One Dimensional Arrays


Manual Page Dynamic Random Variates




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