Dice user, computer;
int count = 0;
int user_score = 0;
int comp_score = 0;
cout << "Welcome to the C++ dice game!" <<endl;
cout << "You are palying against the computer. Each player gets to roll 2 dices a total " <<endl;
cout << "of 6 times. The player with the highest total of dice values will win! " <<endl;
cout << "Are you ready to roll?" << endl;
cout << "Press ENTER key to continue..." <<endl;
keypress = getche();
display_result ( user_score, comp_score);
void display_result( int user, int comp)
if (user > comp)
cout <<"You are the winner!" <<endl;
else if (user < comp)
cout <<"You lose!" <<endl;
else if (user= comp)
cout <<"You have the same score as the computer!" <<endl;
cout <<"Your score is: " << user <<endl;
cout <<"The computer's score is: " << comp <<endl;
please help! every time the roll_dice function get call twice for the same object, i receive the same dice values.
Even though time(NULL) create a distinctive number as seed passed to srand() that creates a random number generator, however, since your program runs so fast, the seed returned by time(NULL) doesn't
get changed in sequential calls to srand().
time(NULL) might get changed every other second, but your code runs much faster than that.
Therefore, two calls to rand() will give you the same random number for both dices in a throw, and that should explain why you always got the same dice value.
So in short, seed ( created by call to time(NULL) ) is the same to both srand() calls, therefore, two rand() calls will give you the same random number.
Mats, this is the exactly what I have meant. What I said was in the context of beginnervn's example above, I probably should have said "the very first rand() call" after calling srand() with a fixed seed will always produce a fixed random number.
Your example should help beginnervn understand a pair of rand() and srand() better.