469,572 Members | 1,281 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,572 developers. It's quick & easy.

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 1379
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

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by Virus | last post: by
10 posts views Thread by Ioannis Vranos | last post: by
23 posts views Thread by Alvin | last post: by
12 posts views Thread by kalinga1234 | last post: by
22 posts views Thread by gagan.singh.arora | last post: by
21 posts views Thread by chico_yallin | last post: by
8 posts views Thread by Anil Gupte | last post: by
16 posts views Thread by jason.cipriani | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.