This question already has an answer here:
- Recommended way to initialize srand? 14 answers
I have a class function int ComputerClass::getGuess() that every time it is called should produce a randomized number. In my main function I created 2 functions of the class ComputerClass Computer1,Computer2 and called the class function, however it returns the same number for each call:cout << Computer1.getGuess()<<endl; cout << Computer2.getGuess()<<endl;
Below is the class member function
`
int ComputerClass::getGuess()
{
//Generate randomn number
srand(time(NULL));
generatednumber = rand() % 100 + 1;
//return computers randomly generated #
cout <<"--(Computer)Please enter a guess between 1-100: "<<
generatednumber<<endl;
return generatednumber;
}`
I'm assuming my issue is the seed value, however I don't know how to set it up so that makes the number random every time it is called.
Aucun commentaire:
Enregistrer un commentaire