Explain random number library

Anyone can explain this ?
I just want to study the codes in deep how the integers randomize

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <cstdlib>
#include <ctime>
#include "random.h"

/*
This function begins by using rand to select an integer in the interval [0, RAND_MAX] and 
then converts it to the desired range by applying the following steps:
1. Normalize the value to a real number in the interval [0, 1)
2. Scale the resulting value to the appropriate range size
3. Truncate the scaled value to an integer
4. Translate the integer to the appropriate starting point.
*/

int RandomInteger(int low, int high) {
    double d = double(rand() / (double(RAND_MAX) + 1);
    int k = int(d * (high - low + 1));
    return low + k;
}
Last edited on
This doesn't generate a random number, it just puts a bound on the random number generated by rand(): http://www.cplusplus.com/reference/cstdlib/rand/
Topic archived. No new replies allowed.