Dice Cup Class Program

Hello cplusplus community, firs post here :). I have run across an issues with one of my functions and was curious to know if there were any hints in regards to where the error lies.

The function is supposed to perform this task :
It should have a method called rollDice that simulates rolling the dice in the cup, returning the total value that was rolled. Remember that if there's more than one die, you need to "roll" each one separately because it's not a uniform distribution (i.e. rolling a single die with the numbers 2 through 12 will not give you the same distribution of numbers as rolling two six-sided dice).

In the client program we than have to:
It should then ask the user how many times they would like to roll those dice and print out the values for that many rolls.


I am not looking for an answer, since for the most part I believe I got everything working correctly, the only issue comes with calling the object in a loop, I keep getting the same value. I even set a random seeding and tried various ways, by including one in the loop, but to no avail I keep getting the same value when the object is called in a loop


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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Class header file
#include <iostream>
#include <ctime>
#include <cstdlib>

#ifndef DICECUP_H
#define DICECUP_H

class DiceCup
{
	private:
		int diceNumber;
		int faceNumber;

	public:
		DiceCup();
		DiceCup(int, int);
		void setDiceNumber(int);
		void setFaceNumber(int);
		int rollDice();

};

#endif


// Class cpp file/implementation file
#include "DiceCup.h"
#include <ctime>
#include <cstdlib>

// Default Construct
DiceCup::DiceCup()
{
	diceNumber = 1;
	faceNumber = 2;
}
// Construct Overload
DiceCup::DiceCup(int x, int y)
{
	setDiceNumber(x);
	setFaceNumber(y);
}
// Mutator Function
void DiceCup::setDiceNumber(int x)
{
	diceNumber = x;
}
// Mutator Function
void DiceCup::setFaceNumber(int y)
{
	faceNumber = y;
}
// Roll Dice Function
int DiceCup::rollDice() // I believe the issue might be here 
{
	srand(time(0));
	int value = 0;

	for (int x = 1; x <= diceNumber; x++) 
		value += rand() % faceNumber + 1;

	return value;
}


// Client Program 
#include <iostream>
#include <ctime>
#include <cstdlib>
#include "DiceCup.h"
using namespace std;

int main()
{
	int dice, faces, rollAmount;

	cout << "How many dice are inside the cup: ";
	cin >> dice;
	cout << "How many faces do each of the dice have ";
	cin >> faces;
	cout << "How many times do you want to roll the dices ";
	cin >> rollAmount;

	DiceCup dice1, dice2(2, 6), dice3(dice, faces);


	cout << endl << "Dice 1 is " << dice1.rollDice() << endl;
	cout << "Dice 2 is " << dice2.rollDice() << endl;


	for (int x = 1; x <= rollAmount; x++)
	{
		cout << "Roll " << x << " is " << dice3.rollDice() << endl;

	}

	return 0;
}


My output in Dice 1 and Dice 2 provide desired results, but as mentioned before Dice 3 provides the same value for each iteration of the loop. Any hints would be greatly appreciated. thanks


How many dice are inside the cup: 3
How many faces do each of the dice have 10
How many times do you want to roll the dices 5

Dice 1 is 2
Dice 2 is 9
Roll 1 is 26
Roll 2 is 26
Roll 3 is 26
Roll 4 is 26
Roll 5 is 26
Press any key to continue . . .
Last edited on
Line 57: You don't want to call srand() there. Every time you roll, you're going to call srand() which will reset the RNG to return the same set of numbers (if called within the same second). srand() should be called only ONCE at the beginning of main.


Hey thank you very much, I still can not wrap my head around why that is. So to be clear basically the random seeding inserted in srand = time(0) is always resetting back to zero when the function is called I presume; but last time I checked the time function is never the same during the program.

BTW here is the desired output

How many dice are inside the cup: 2
How many faces do each of the dice have 10
How many times do you want to roll the dices 4

Dice 1 is 1
Dice 2 is 7
Roll 1 is 7
Roll 2 is 9
Roll 3 is 16
Roll 4 is 18
Press any key to continue . . .
I said
if called within the same second
. When you call rollDice() at line 94, you're calling it within a loop, therefore within the same second.

The reason is that srand() takes a seed as an argument. time(0) is only accurate to the second. Therefore, when calling srand() multiple times within the same second, you're using the same seed. When srand() is called with the same seed, the RNG is set back to the same place. Subsequent calls to rand() will then return the same sequence of numbers.

Thanks a lot AbstractionAnon, makes perfect sense now. Once I wrote it down like this(is incorrect of course), everything is clear, just as you said it is taking the same seed for each iteration; which is why having it outside produces the correct results.

1
2
3
4
5
6
for (int x = 1; x <= rollAmount; x++)
	{
		srand(time(0)); // but I could have done this srand(x); if used inside the loop!
		cout << "Roll " << x << " is " << dice3.rollDice() << endl;

	}


Again thank you for the information.
Topic archived. No new replies allowed.