I'm trying to get randomly generated numbers by using srand()
and rand()
but with no avale: every time I run the program it increases by a certain amount of numbers. But if I use a for
statement, no pattern can be seen.
For example, if I use the code provided below and run and close the program 10 times, the output will be:
6
32
64
91
13
62
...
Note: one strange thing I noticed about this that when I unfocus or minimize Visual Studio
and close the Command Prompt, the next time I run the program the number increases by more than 20, but, if I don't unfocus or minimze Visual Studio
the number increases by just little over 1-5. Why's that?
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand (time(NULL));
int random_number = rand () % 100 + 1;
cout << "Random number: " << random_number << endl;
return 0;
}
But, if I use this code:
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand (time(NULL));
for (int i = 0; i < 10; i++) {
int random_number = rand () % 100 + 1;
cout << "Random number: " << random_number << endl;
}
return 0;
}
the numbers don't have a clear pattern and I get the output:
31
10
81
66
74
14
6
97
39
23
They increase and decrease randomly by random amounts. What seems to be the problem here?
Aucun commentaire:
Enregistrer un commentaire