### Dice Rolls

I wanted to create a simple program that rolls dice randomly and measures what dice face comes up, the frequency, and overall percentage of each face. It's for a six-sided dice. I haven't really done much with random numbers before. So, output would be something like -

face frequency percentage
1 8 22%
2 5 12.5%

and so on. I don't I've gotten very far though.

 ``1234567891011121314151617181920212223242526272829303132333435`` ``````// L03Random1.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { int d1; int d2; int d3; int d4; int d5; int d6; unsigned seed; int i; cout << "How many times do you want to throw the dice (0 or less will stop the program)? "; cin >> seed; while (seed > 0) { srand(seed); for (i=0; i <= seed; i++) { cout << setw(6) << rand(); cout << endl; } } return 0; }``````
1. You are seeding with the number of times you want to throw the dice? That means that if you throw the dice 10 times, you'll ALWAYS get the same sequence output. Put `srand( time(NULL) );` at the start of `int _tmain()` and delete it at line 27.

2. line 30 will display rand, but then the number is lost. Next time you call rand(), it'll be a different number. Always store your random number so that you can use it later.

3. You aren't counting the frequency of each face.

You can satisfy 2 & 3 with this (assuming you initialize d1 thru d6 to 0)
 ``12345678910111213`` ``````for (i = 0; i <= seed; i++) { int generatedNum = rand(); switch (generatedNum) { case 1: d1++; break; case 2: d2++; break; case 3: d3++; break; case 4: d4++; break; case 5: d5++; break; case 6: d6++; break; } }``````

Your random number will be something between 0 and RANDMAX. Try this to get a number between 1 and 6:
`rand()%spread+min;` where spread is 6 and min is 1... so
`rand()%6+1;`
So, something more like this? I'm not sure where to put rand()%6+1; Also, what does the break do?

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647`` ``````// L03Random1.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { srand( time(NULL) ); int d1 = 0; int d2 = 0; int d3 = 0; int d4 = 0; int d5 = 0; int d6 = 0; unsigned seed; int i; cout << "How many times do you want to throw the dice (0 or less will stop the program)? "; cin >> seed; if (seed > 0) { for (i = 0; i <= seed; i++) { int generatedNum = rand(); switch (generatedNum) { case 1: d1++; break; case 2: d2++; break; case 3: d3++; break; case 4: d4++; break; case 5: d5++; break; case 6: d6++; break; } } cout << "Face Frequency Percentage" << endl; cout << "d1 " << d1 <<" " << d1 / seed << endl; } else return 0; }``````
Or you could use an alternate method that will make calculating your stats programmatically a bit easier:

 ``123456789101112131415161718192021`` ``````#include #include #include using namespace std; int main() { // array of elements representing the different faces on a die. // element 0 corresponds to a die roll of 1, 1 to 2, 3 to 4, 4 to 5 and 5 to 6. int die_face[6] = {} ; // all elements initialized to 0. srand(time(NULL)) ; unsigned iterations = 0 ; cout << "How many times do you want to throw the dice (0 or less will stop the program)? "; cin >> iterations ; for ( unsigned i=0; i
The code I wrote doesn't seem to work though. All the dice frequency and percentage always equals zero.
Last edited on
Well, you're only showing the frequency of 1 die face and you're using integer division to calculate the percentage.

 ``12`` ``````for each die face cout << die_face# << die_face_freq << " " << static_cast(die_face_freq)/n_iterations << '\n' ``````

substitute the expressions that make sense in your code.

Edit: I see you didn't adjust the generated number from rand to be one of the mapped values. That's a problem.
Last edited on
I think I got it now. However, I can't seem to get the results to line up. Like if frequency is double digits for some and single digits for others, then percentage alignment is mixed up. The cursor starts farther to the right for some.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````// L03Random1.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #include #include #include using namespace std; int _tmain(int argc, _TCHAR* argv[]) { srand( time(NULL) ); int d1 = 0; int d2 = 0; int d3 = 0; int d4 = 0; int d5 = 0; int d6 = 0; double seed; int i; cout << "How many times do you want to throw the dice (0 or less will stop the program)? "; cin >> seed; if (seed > 0) { for (i = 0; i <= seed; i++) { int generatedNum = rand()%6+1; switch (generatedNum) { case 1: d1++; break; case 2: d2++; break; case 3: d3++; break; case 4: d4++; break; case 5: d5++; break; case 6: d6++; break; } } cout << "Face Frequency Percentage" << endl; cout << "d1 " << d1 <<" " << (d1 / seed)*100 << endl; cout << "d2 " << d2 <<" " << (d2 / seed)*100 << endl; cout << "d3 " << d3 <<" " << (d3 / seed)*100 << endl; cout << "d4 " << d4 <<" " << (d4 / seed)*100 << endl; cout << "d5 " << d5 <<" " << (d5 / seed)*100 << endl; cout << "d6 " << d6 <<" " << (d6 / seed)*100 << endl; } else return 0; }``````
 ``1234567`` ``````cout << "Face \t Frequency \t Percentage" << endl; cout << "d1 \t " << d1 <<" \t\t " << (d1 / seed)*100 << endl; cout << "d2 \t " << d2 <<" \t\t " << (d2 / seed)*100 << endl; cout << "d3 \t " << d3 <<" \t\t " << (d3 / seed)*100 << endl; cout << "d4 \t " << d4 <<" \t\t " << (d4 / seed)*100 << endl; cout << "d5 \t " << d5 <<" \t\t " << (d5 / seed)*100 << endl; cout << "d6 \t " << d6 <<" \t\t " << (d6 / seed)*100 << endl;``````

Try something like this. Instead of using a ton of consecutive spaces, you can use a tab by putting `"\t"` in there. That will align itself every so often. If you still have problems, then you'll need to look into the <iomanip> header at the setw function.
http://cplusplus.com/reference/iostream/manipulators/setw/

To answer your question earlier, the `break `statement gets you out of the `switch ` control.
Last edited on
Topic archived. No new replies allowed.