#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int random_Number();
int random_One();
int main(){
cout << "You have : " << random_Number() << endl;
cout << "The dealer has : " << random_Number() << endl;
}
int random_Number(){
int v1, v2;
srand((unsigned)time(NULL));
v1 = rand() % 10 + 1;
v2 = rand() % 10 + 1;
return v1 + v2;
}
If I understand correctly, srand() is a seed which will change each time I run the program since the time changes and therefore so does srand()
. However, since time does not change once the program has been started, the program will keep using the same seed and thus generating the same numbers over and over.
So, if I want my function random_Number
to generate a new random number every time it is called, then I must also change the seed. I assume you need to set your seed to something else than time, but I don't know what else? It's a bit paradoxical that you can't loop the seed, since that would require a random number for each iteration - which is exactly the problem I have.
Is there a better way to do this? And if not, how would you change the seed so that it varies every time the function is called?