In C/C++, the
rand() function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX. This function return a pseudo-random integer, not true.
The
srand() function uses the argument as a seed for a new sequence of pseudo-random numbers to be returned by subsequent calls to rand(). If srand() is then called with the same seed value, the sequence of pseudo-random numbers shall be repeated. If rand() is called before any calls to srand have been made, the same sequence shall be generated as when srand() is first called with a seed value of 1.
In this exercise, current date and time, time(0), is used as seek for srand(). Such that the generated pseudo-random numbers will be a more random.
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
cout << "Random number generated by rand()" << endl;
for(int i=0; i < 10; i++){
cout << rand() % 100 << " ";
}
cout << endl;
cout << "Random number generated with srand()" << endl;
srand(static_cast<unsigned int>(time(0)));
for(int i=0; i < 10; i++){
cout << rand() % 100 << " ";
}
cout << endl;
return 0;
}