### 2.5.1. The integer mode

We want to take advantage of the class random_source to simulate a fair dice, that is, a dice whose 6 numbers all are thrown with the same probability. For this, we execute the following experiment: We throw one million times, count in an array which number is thrown how often, and output the relative frequencies at the end.

So the random source must create an integer number between 1 and 6 at every call. We therefore use it in integral mode.

```#include <LEDA/array.h>
#include <LEDA/random_source.h>
#include <iostream>

using leda::array;
using leda::random_source;

int main()
{

random_source S(1,6);

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;
S >> random_number;
counter[random_number]++;
}

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

}
```
Note Here it shall be noted once again that arrays must be initialized explicitly. LEDA does not initialize the elements of the above array counter to 0 already at the definition!

A run of the program on the machine of the author output the following values

```1 : 0.167126
2 : 0.167223
3 : 0.166626
4 : 0.166339
5 : 0.166554
6 : 0.166133
```

As we see, every number was thrown approximately with the relative frequency 1/6. This shows that random_source creates uniformly distributed random variables.

A random source expects as parameters of its definition the integral bounds lb and ub of the interval which it will create integral random numbers from with an equal relative frequency later:

`random_source S(lb, ub);`

This interval is called its range.

Creating a random number is carried out by the operator >>:

`S >> r;`

The random source then sets the value of r to a random number from the interval specified at the definition.

This interval can be newly defined by a call of the method

`S.set_range(new_lb, new_ub);`

Furthermore, a random number from a certain interval [lb, ub] can be created also directly from an already defined random source S by the call

`int random_number = S(lb, ub);`

#### The seed

The random numbers are created by a certain algorithm. The question arises how this algorithm creates its very first number or what the original parameter of the algorithm is. This parameter is referred to as the seed. All following numbers are calculated starting out from this seed. By using the same seed again, the same random sequence can be created again and again because internally, the algorithm creates the respectively next number from the previous one. This seed can be specified by the method

`S.set_seed(s);`

which expects an integer number s as argument. Using the same seed can be very useful during debugging because the program then always takes the same decisions in the same lines and therefore runs deterministically.

We have not defined any seed here. Is the same output always created now? We let it run once again:

```1 : 0.167507
2 : 0.166057
3 : 0.167237
4 : 0.166658
5 : 0.166504
6 : 0.166038
```

No, this output is different from the one above. This is caused by the fact that random_source sets itself the seed if none is specified. To get a more or less random value for this, it uses the system time as one of the parameters in the computation of the seed.

Tip The random source rand_int is already pre-defined in the header file random_source.h. So it can be used without an explicit definition everywhere. Its range is pre-defined to the interval [0..231-1].