473,884 Members | 2,313 Online

# Trouble calculating percentage in C++ dice program

41 New Member
I'm writing a program that will have to roll the dice A and dice B one million times, and calculate the percentage of times that the dies will be equal.

I'm having trouble to figure out how the percentage will work.

Here is my code:
Expand|Select|Wrap|Line Numbers
1.
2. #include <ctime>
3. #include <iostream>
4. using namespace std;
5.
6. const double roll = 1000000;
7.
8. int rolldie() //statement will give u 6
9. {
10. return (rand()%6+1);
11. }
12.
13. void Randomize()
14. {
15. srand( (unsigned)time( NULL ) ) ;
16. }
17.
18. int main()
19. {
20.     int diceA;
21.     int diceB;
22.     int totals;
23.     int i;
24.     cout << "Rolling dices one million times\n";
25.     Randomize();
26.
27.     for(i=0;i<roll;i++)
28.     {
29.         diceA = rolldie();
30.         diceB = rolldie();
31.         int    totals = diceA + diceB;
32.         totals++;
33.     }
34.
35.     if(diceA==diceB)
36.     {
37.         cout << "Doubles occurred" << totals / 1000000 << "percent of the time.";
38.     }
39.
40.     return 0;
41. }
42.

Doug
Aug 31 '07 #1
38 9160
sicarie
4,677 Recognized Expert Moderator Specialist
Expand|Select|Wrap|Line Numbers
1.
2.     for(i=0;i<roll;i++)
3.     {
4.         diceA = rolldie();
5.         diceB = rolldie();
6.         int    totals = diceA + diceB;
7.         totals++;
8.     }
9.
10.
Okay, let's walk through this for two arbitrary dice rolls.

for (i = 0; 0 < 1000000; 0++)
diceA = 3 //made this up
diceB = 4 // made this up too
totals = 3 + 4 // so totals = 7
totals++ // so totals = 8

for (i =1; 1< 1000000; 1++)
diceA = 1
diceB = 5
totals = 1+5 // totals = 6
totals++ // totals = 7

Do you see the issue here? Not only does totals get wiped out every iteration through the loop, but you also don't get the right total amount because of the increment.

I think you want it to be

totals += diceA + diceB

and no totals++. ::Edit:: and you might want to move the 'int totals' out of the loop. It will probably be seen as a re-declaration of the variable each time, in which case you'll get undefined behavior, and who knows what you will get.

I also changed the title to be a bit more descriptive - draw a few more people in, if they have something to comment on.
Aug 31 '07 #2
JosAH
11,448 Recognized Expert MVP
Expand|Select|Wrap|Line Numbers
1.     for(i=0;i<roll;i++)
2.     {
3.         diceA = rolldie();
4.         diceB = rolldie();
5.         int    totals = diceA + diceB;
6.         totals++;
7.     }
8.
Shouldn't you check *inside* that loop whether or not diceA equals diceB?
If they're equal you should increment a counter ('totals' mayhap?)

When the loop is finished you have rolled the dice 'roll' times and 'totals'
times they were equal.

kind regards,

Jos
Aug 31 '07 #3
d0ugg
41 New Member
Thank you so much,
That was very fast,

I will try that right now and let you guys know what happened.

Doug
Aug 31 '07 #4
sicarie
4,677 Recognized Expert Moderator Specialist
Shouldn't you check *inside* that loop whether or not diceA equals diceB?
If they're equal you should increment a counter ('totals' mayhap?)

When the loop is finished you have rolled the dice 'roll' times and 'totals'
times they were equal.

kind regards,

Jos
Does the OP even need the check to see if they are the same? Why does the OP care if they are the same? They should be dealt with the same way, right?
Aug 31 '07 #5
kreagan
153 New Member
Does the OP even need the check to see if they are the same? Why does the OP care if they are the same? They should be dealt with the same way, right?
The object of the assignment was to find the probability of doubles thrown. Your suggestion is for the average number thrown.
Aug 31 '07 #6
sicarie
4,677 Recognized Expert Moderator Specialist
The object of the assignment was to find the probability of doubles thrown. Your suggestion is for the average number thrown.
Ah, good catch.
Aug 31 '07 #7
JosAH
11,448 Recognized Expert MVP
Ah, good catch.
You should try a different brand of coffee ;-)

kind regards,

Jos (<--- Italian 'Illy' espresso)
Aug 31 '07 #8
sicarie
4,677 Recognized Expert Moderator Specialist
You should try a different brand of coffee ;-)

kind regards,

Jos (<--- Italian 'Illy' espresso)
Yeah, I'm used to my wife's coffee. She's a nurse, and when people like that work 12 hour shifts, they tend to make it very very strong. Then I come to work and I'm drinking 3 cups and just starting to feel awake... (Hence the attempt at cutting back)
Aug 31 '07 #9
d0ugg
41 New Member
So,
I did what you told me

Expand|Select|Wrap|Line Numbers
1. for(i=0;i<roll;i++)
2.     {
3.         diceA = rolldie();
4.         diceB = rolldie();
5.         totals += diceA + diceB;
6.     }
7.
But not it seems that is running, but it's not printing anything.
And I also put the int totals out of the loop..

Any ideas why is not printing the percentage?

Thank you so much for the help,

Doug
Aug 31 '07 #10