Hello!
So I am trying to populate 3 vectors with random numbers, but it keeps generating 51 as my random number and no others. I am not sure what I am doing wrong.
vector<int> v1;
for (int i = 0; i <=5000; i++)
{
srand(time(0));
int b = rand() % 100;
v1.push_back (b);
cout << v1[i] << endl;
}
vector<int> v2;
for (int j = 0; j <=10000; j++)
{
srand(time(0));
int b = rand() % 100;
v2.push_back (b);
cout << v2[j] << endl;
}
vector<int> v3;
for (int k = 0; k <= 20000; k++)
{
srand(time(0));
int b = rand() % 100;
v3.push_back (b);
cout << v2[k] << endl;
}
This calls the function rand() from the file #include<cstdlib>
3 individual times. Creating 3 different numbers between 0 and 99 and pushing them back into the first element of the 3 vectors.
You said
DESCRIPTION OF CONCEPTS
.
Well, here is the concept of SEEDING explained:
srand(time(0));
^
A computer does not generate random numbers, nor does it generate random numbers. That's right. In this specific universe, there is no such thing as "random".
A computer initially READS from a pre-determined LIST of numbers (Which I for the time being believe are incremented ascendingly), what your are doing when you are seeding the computer, you are telling the computer to begin reading from a specific place in said LIST, based on the current TIME and DATE of your PC - now, because every moment(Long integer value) is different on your PC, every time you begin the program, the PC will also start reading from somewhere else - giving you a different set of numbers to read from.