473,378 Members | 1,504 Online

# programming help, random number generation

I need help on getting started with a dice program that will output as many random numbers from 1 to 6 and as many rolls as the user requests, and then how many times each number shows up from the number of rolls (occurrences)...and the corresponding percent. I'm supposed to use a loop (while, for) and some "do". . . I'm lost. Please help!

So far, this is what I have. . .
Expand|Select|Wrap|Line Numbers
1. #include <stdio.h>
2. #include <stdlib.h>
3. #include <time.h>
4.
5.
6. int main ()
7. {
8.   int x;
9.   int Rolls;
10.   int DiceNumber;
11.   int Number;
12.   int FinalRoll;
13.   int D1;
14.   int D2;
15.   int D3;
16.   int D4;
17.   int D5;
18.   int D6;
19.   double Occurances;
20.   double Percent;
21.
22. //Initialize Random Seed
23.   srand ( time(NULL) );
24.
25. //Rolling die
26.   Number = rand() %6 + 1;
27.
28.     x = 0
29.     D1 = 0
30.     D2 = 0
31.     D3 = 0
32.     D4 = 0
33.     D5 = 0
34.     D6 = 0
35.
36.   do {
37.       printf("Enter the number of times you want to roll the die:\n");
38.       scanf("%d", &Rolls);
39.       if
40.       while (x < Rolls) {
41.
42.     Percent = Occurances / Rolls
43.     printf("%6c,%3d,%6c%3d%8c%7.2f\n", DiceNumber, ' ', Occurances, ' ', Percent);
44.
45.   return 0;
46. }
Feb 19 '08 #1
1 1512
RRick
463 Expert 256MB
The best way to deal with a program like this is one issue at a time.

As for your do-while loop, its all messed up. My suggestion is ignore the "do" part and replace it with a single while loop. It will look something like while( xxx) { ... }, where xxx is the conditional (you got that right) and ... is the code.

You got Number right, but you only generate a single one. You want to generate "Rolls" number of them, so you need to put that inside the while loop.

You need to store the values of Number somewhere. I suspect that what D1 ... D6 are for. If you want to use them, you will have to create a big if-else strucure that checks each possible value for Number and then up dates the correct variable. You could instead use an array (with 7 not 6 items) and just update each cell in the array by using Number as an index to the array. Both ways work, but one uses a lot less code.

Finally, you need to print out the results. This is done after the while loop.
Feb 20 '08 #2