dice roll problem

Dec 11, 2008 at 12:41am
hello im fairly new to programing and i need help with his programing question
Ima making a program to simulate the rolling of dice.I got most of it to work i just need help on setting a specific amount of times for the dice to be rolled
for example i want to set the amount of time i roll the dice to 20.After 20 rolls the program should output

1 was rolled 5 times
2 was rolled 6 times
ect.
ect.
but i need help to keep the total rolls at 20

here is my program so far

______________________________________________________________________________


#include <cstdlib>
#include <iostream>
#include <cmath>
#include <ctime>

using namespace std;

int main()
{
const int Rolls_of_Dice = 10;
const int Sides_of_Dice = 6;
srand (time(NULL));

int counter = 0;

for (int i =0;i < Sides_of_Dice; i++)
{
int counter = rand() % 6 + 1;
cout<< i + 1<<" was rolled "<<counter<<" times"<<endl;

cout<< endl;
}
system("PAUSE");
return EXIT_SUCCESS;
}
Dec 11, 2008 at 2:19am
mate, I don't completely understand, what u'r trying to do is mainly for the program to roll 20 dices and then display?
Dec 11, 2008 at 2:34am
NO I NEED IT TO ROLL THE DICE 20 TIMES AND RECORD THE RESULTS OF HOW MANY TIMES IT LANDED ON EACH SIDE
Dec 11, 2008 at 3:03am
the code worked for me rolling 6 times, because that's what u set, I took it and fixed it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
srand (time(NULL));
int counter = 0;
for (int i =0;i < 20; i++)
{
int counter = rand() % 6 + 1;
cout<< i + 1<<" was rolled "<<counter<<" times"<<endl;
}
system("PAUSE");
}

that code works perfectly, rolling 20 times

actually you don't need any variable



Now, if what you want is to count how many times it landed on each side, i'd recommend u to try a switch statement
Last edited on Dec 11, 2008 at 3:05am
Dec 11, 2008 at 3:06am
A couple of problems with your program.

1. You're for loop only loops through 6 iterations which is Sides_of_Dice. If you are looking to roll the dice 20 times (20 loop iterations) you should change that constant Rolls_of_Dice, which you aren't using, to 20. Plug that into your for loop instead of the Sides_of_Dice and that'll give you 20 loops.

2. The following line does not count how many times the die landed on each side:

cout<< i + 1<<" was rolled "<<counter<<" times"<<endl;

What you need to do is create say 6 variables, 1 for each die to count how many times it has been rolled:

1
2
int count1 = 0;
int count2 = 0;

etc...

After each random number is generated, check if counter == the specific number and then increment the count of that number. So for example say the die rolls a 2. You may want to say:

1
2
if ( counter == 1 )
     count1++;


Once your for loop ends (die is rolled 20 times), you can then display how many times each number was rolled:

 
cout << "1 was rolled " << count1 << " times." << endl;


Hope this helps.

Return 0;

EDIT: Also, you might as well toss those constant variables out and just use 20 and 6. I think what you originally planned to do was use the Sides_of_Dice to create the limits for your random number generation. Both are unnecessary for this program.
Last edited on Dec 11, 2008 at 3:11am
Topic archived. No new replies allowed.