Real noob here. Started learning C++ as of this week. Trying to create a dice rolling program where player rolls 3 dice and so does the computer. The one who has the highest dice values scores points etc ...
I think I made a function that handles the dice rolling but when I invoke the function for the human player and afterwards for the computer I always get the same result for both. Something definitely wrong and I would appreciate it if someone had a look at my (faulty) code. Can't find the error and it's frustrating. I know the function could be more concise
I have made an alternate version with a do loop and switch case for the dievalues but the outcome is just the same as below. Both players end up with the same result (dieValueSum) ...
computerDice = rollDice(); and
playerDice = rollDice(); are always the same ???
Your rollDice function seeds the random number generator with the current time.
Then you call rand three times. They're not random, they're just three numbers that you get when you seed with whatever particular value you seeded with. That's how it works. You give it a starting value (using srand) and then it calculates each new rand value from there. Same seed means same values.
Then, you call rollDice again. The rollDice function seeds the random generator again, with the EXACT same seed value, as the time hasn't changed. Same second. So then your three calls to rand give you back the exact same three values, because you seeded the generator again, with the exact same seed.