srand does not produce random numbers. It seeds (initializes) the random number generator used by rand.
You do it like this:
1 2 3 4 5 6 7 8 9 10
int main()
{
// seed the random number generator once and only once
srand( time(0) );
// now, when you need random numbers, you can call rand()
int random = rand();
cout << random;
}
I find it a little implausible that rand is producing predictable results. Just because you ran it twice and both times the numbers were kind of similar doesn't mean they'll always be that way. But whatever.
One thing you could try would be to "scramble" the seed by reseeding with rand output:
1 2 3 4
seed( time(0) ); // initial seed
seed( rand() ); // scramble it
// now try using rand
Another option might be to throw away the first few outputs of rand:
1 2 3 4 5 6 7
seed( time(0) );
// throw out the first 5 numbers in the sequence
for(int i = 0; i < 5; ++i)
rand();
// now try using rand
Of course, these "fixes" are completely superfluous and will do very little (read: nothing) to make the rand sequence more random. All they do is change where the sequence starts.
//Random Number Generator
srand(time(0) );
int random = rand();
//End of random number generator
if(JobChoice == "A" || JobChoice == "a"){
cout << "Our printers broke so we need you to " << endl;
cout << "type out these codes for us as many times as you can." << endl;
cout << random;
cout << "\n";
getline(cin, Typing);
}
while(JobChoice != "Back" || JobChoice != "back"){
getline(cin, Typing);
if(Typing == random){
money +=7;
cout << "Money +7 " << money << endl;
cout << "\n";
}
if(Typing == "Back" || Typing == "back"){
MainMenu();
}
what i want to do is have the number generator generate a random number which it does, then i want the user to have to type in what was generated to earn money, but it wont work and i dont get why? it the line: if(Typing == random) i thought its basically saying if what you type in (Typing) equals what was generated give $7. Why wont it work?