Roll dice function rand() always gives same result

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 ???

Thanks in advance. Madmacker

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <cstdlib>
#include <ctime>


using namespace std;

int rollDice ();

int main()
{
	int computerDice = 0;
	int playerDice = 0;
	computerDice = rollDice();
	cout << "\ncomputer rolled: " << computerDice << endl;
	cout << endl;
	playerDice = rollDice();
	cout << "\nplayer rolled: " << playerDice << endl;
	
	return 0;
}


int rollDice()
{
	int die1 = 0;
	int die2 = 0;
	int die3 = 0;	
	int dieValue1 = 0;
	int dieValue2 = 0;
	int dieValue3 = 0;
	int dieSum = 0;
	int dieValueSum = 0;	
	
	srand(static_cast<unsigned int>(time(0)));
		die1 = rand() % 6 + 1;
			if (die1 == 1) dieValue1 = 100;
			if (die1 == 2) dieValue1 = 2;
			if (die1 == 3) dieValue1 = 3;
			if (die1 == 4) dieValue1 = 4;
			if (die1 == 5) dieValue1 = 5;
			if (die1 == 6) dieValue1 = 60;
			cout << die1 << " - ";
		die2 = rand() % 6 + 1;
			if (die2 == 1) dieValue2 = 100;
			if (die2 == 2) dieValue2 = 2;
			if (die2 == 3) dieValue2 = 3;
			if (die2 == 4) dieValue2 = 4;
			if (die2 == 5) dieValue2 = 5;
			if (die2 == 6) dieValue2 = 60;
			cout << die2 << " - ";
		die3 = rand() % 6 + 1;
			if (die3 == 1) dieValue3 = 100;
			if (die3 == 2) dieValue3 = 2;
			if (die3 == 3) dieValue3 = 3;
			if (die3 == 4) dieValue3 = 4;
			if (die3 == 5) dieValue3 = 5;
			if (die3 == 6) dieValue3 = 60;
			cout << die3 << endl;																		
		dieSum = die1 + die2 + die3;
		dieValueSum = dieValue1 + dieValue2 + dieValue3;
		
	cout << "\nThe Sum of the 3 dice rolled is " << dieSum << endl;
	cout << "\nThe Sum of the values of 3 dice rolled is " << dieValueSum << endl;
	return dieValueSum;

}
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.

JUST SEED THE GENERATOR ONCE.

Also, don't use rand(), it's awful. Here's a whole video about how bad it is; https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful
Last edited on
Topic archived. No new replies allowed.