programming help, random number generation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jackspam
    New Member
    • Feb 2008
    • 1

    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. . .
    [code=c]
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>


    int main ()
    {
    int x;
    int Rolls;
    int DiceNumber;
    int Number;
    int FinalRoll;
    int D1;
    int D2;
    int D3;
    int D4;
    int D5;
    int D6;
    double Occurances;
    double Percent;

    //Initialize Random Seed
    srand ( time(NULL) );

    //Rolling die
    Number = rand() %6 + 1;

    x = 0
    D1 = 0
    D2 = 0
    D3 = 0
    D4 = 0
    D5 = 0
    D6 = 0

    do {
    printf("Enter the number of times you want to roll the die:\n");
    scanf("%d", &Rolls);
    if
    while (x < Rolls) {

    Percent = Occurances / Rolls
    printf("%6c,%3d ,%6c%3d%8c%7.2f \n", DiceNumber, ' ', Occurances, ' ', Percent);

    return 0;
    }[/code]
    Last edited by sicarie; Feb 20 '08, 02:48 PM. Reason: Code tags
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    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.

    Comment

    Working...