### 2.6.1. Static random variables (class `random_variate`)

Learning objectives

 The class `random_variate`

Who is unlucky in love should at least be lucky at cards. And to the one who is that neither, it be allowed to influence the gamble in his favor. In this sense, let us simulate a loaded dice with which the number “6” is thrown with probability 5/25, but every other number merely with probability 4/25.

This is a non-uniformly distributed random variable whose distribution we know in advance and that does not change. We take advantage of the LEDA class `random_variate` to create the values of this random variable. Like in the experiment in Section 2.5.1 we throw the dice one million times and count which number was thrown how often:

LEDA users as of version 5.0: Note that header files are now partitioned into modules (subfolders)!
```#include <LEDA/random_variate.h>
#include <LEDA/array.h>
#include <iostream>

using leda::array;
using leda::random_variate;

int main()
{
array<int> weight(1,6);

weight[1] = 4;
weight[2] = 4;
weight[3] = 4;
weight[4] = 4;
weight[5] = 4;
weight[6] = 5;

random_variate R(weight);

array<int> counter(1,6);
for(int i = 1; i <= 6 ; i++) counter[i] = 0; // necessary!

const int num_rolls = 1000000;

for(int i = 0; i <= num_rolls ; i++) {
int random_number = R.generate();
counter[random_number]++;
}

for(int i = 1; i <= 6 ; i++) {
std::cout << i << " : ";
std::cout << static_cast<double>(counter[i]) / num_rolls << "\n";
}
}
```

A program run output the following values:

```1 : 0.160665
2 : 0.159366
3 : 0.160527
4 : 0.160038
5 : 0.159315
6 : 0.20009```

This shows that the values were created in accordance with the desired distribution.

The class `random_variate` expects a LEDA array at the definition in which the weights of the distribution to be created are stored:

`random_variate R(weight);`

Every single (index-)value of the array is then created with a probability that is equal to this weight divided by the grand total of all weights. The grand total is 25 in the above case; the weight of the random value “6” is 5, the weight of all other numbers is 4.

A single value is created by a call of the method

`R.generate();`

Tip `random_variate` can only create integral random values that all are indices of the underlying weight array. To create arbitrary, even non-integral random values, we can, for example, use the integer numbers created by `generate()` as indices of a second array that contains the values to be actually created.

#### Further information

More information about the class `random_variate` can be found on the corresponding manual page.

#### Exercises

 Exercise 18. In Conway's game of life we start, until now, with always the same original population. Populate the original rectangular world with a random population. Experiment with the weights of the distribution.