Ever played an FPS game and wondered how the gun's bullets are almost never on spot with your aim? Maybe a gambling game that has you rolling a dice where you have a "chance" of achieving a certain number? The computer does this by using a random number generator. Of course,
these numbers are not truly random by any means. The numbers are generated (hence the name) based on a seed. If you played Minecraft and noticed how entering the same seed gives you the same map every time, that is the exact concept all RNGs use.
Introducing srand() and rand()
To utilise the RNG, you must specify a seed with "srand()". Afterwards, you can call "rand()" and it will generate a number for you. Let's generate a few:
Code (C++)
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
//Set the RNG's seed to 2 and generate a few numbers
srand(2);
for (int i = 0; i < 5; i++) {
cout << rand() << endl;
}
}
The output might look like this (You may get different numbers)
Output
1505335290
1738766719
190686788
260874575
747983061
Generating numbers between certain values
There are a few ways you can do this. Let's say you wanted to generate numbers between 0 and 20 (excluding 20), you can utilise modulo division:
Code (C++)
int value = rand() % 20;
Now, if you wanted to generate numbers between 10 and 20 (again, excluding 20), we can modify the formula a little:
Code (C++)
int value = 10 + rand() % (20 - 10);
What about actually including 20 though? Then we will do this:
Code (C++)
int value = 10 + rand() % (20 - 10 + 1);
See how it is working? "20 - 10 + 1" is "11", and "rand() % 11" can go up to 10 without going back down to 0. Then we simply add 10 to it. This will force the value to be between 10 and 20.
Overall, your formula for generating numbers including the bounds is:
Code (C++)
int value = MIN + rand() % (MAX - MIN + 1);
How seeding really works
If you specify the same seed, you will get the same "random" numbers. For instance:
Code (C++)
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
srand(2); //Set the RNG's seed to 2.
cout << rand() << endl; //Print out generated value
srand(2); //Set the RNG's seed to 2 again
cout << rand() << endl; //Print out generated value... again
}
If you noticed, the output will be the same for both couts, even though we called rand() twice!
Output
1505335290
1505335290
The reason behind this is because the RNG is like a mathematical equation. If you think of a linear equation in math, like y = 2x + 1, at y(0), you will get 1. At y(1), you will get 3, etc. The random number generator starts at position 0 of some bizarre function, and goes up on every call of "rand()". But if you call "srand()", it will go back down to 0. And if the seed is the same, it will generate the same numbers because it will run across the same equation. That is why it generates the same numbers if the same seed is given.
So if you are wondering why you are generating the same numbers constantly, take a look at if you are calling "srand()" more than once in your program. That is likely the reason why.