Dice roll generator

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Metalman
    New Member
    • Apr 2012
    • 5

    Dice roll generator

    Hello all

    So what am trying to do is write a program that will simulate the rolling of a single dice using rand() and time.h.

    1)The user will input the number of times he wants the dice to be rolled.
    2)The generator will roll the dice X times and count the times each one of 1-6 number was generated and save them in an array.
    3)It well then count the percentage each of the 6 numbers was generated and save it in another array.
    4)It will then find the minimum and maximum percentage and subtract max-min and if its more than 5% it will print a message that says the generator is faulty.

    I need to make a histogram for each number with +,- axis and stars but I really haven't gotten there yet cause it definitely sounds scary.

    So I hope I made everything clear.
    This is my code which gives awkward results.
    P.S It compiles ok but the results are strange.

    Code:
     #include <stdio.h> 
    #include <stdlib.h>
    #include <time.h>
    
    int random_number();
    float calc_percentage(int totals, int nums);
    float calc_maxper(float perc[6]);
    float calc_minper(float perc[6]);
    float permin;
    float permax;
    
    
    int main(void)
    {
        int nums;
        int i;
        int totals[6] = {0};
        float percentages[6] = {0};
        
        srand(time(NULL));
        
        printf("How many numbers to generate?");
        scanf("d%", &nums);
        
    for (i = 1; i <= nums; i++)
    	{
    	    int x = random_number();
    	    totals[x-1]++;   
           printf("%d", x);
    	}
    	
    for (i = 0; i<6; i++)
    {
        percentages[i] = calc_percentage(totals[i],nums);
        printf("The percentage of each number is: %.2f%\n", percentages[i]);
    }
    
    
    permin = calc_minper(percentages);
    permax = calc_maxper(percentages);
    
    if (((permax) - (permin)) > 5)
       printf("According to the percentages of each number the generator is diss-functional\n");
       printf("%.2f\n", permin);
       printf("%.2f\n", permax);
       
    system("pause");
    return 0;
    }
    
    
    
    int random_number()
    {
        int randnum;
        randnum = 1 + (rand() % 6);
        
        return randnum;
    }
    
    float calc_percentage(int totals, int numbers)
    {
          float a;
          
          a = (totals * 100)/numbers;
          
          return a;
    }
    
    float calc_minper(float perc[6])
    {
           int i;
           float min;
           min = perc[0];
           
           for (i=1; i<6; i++)
               {
                     if (perc[i] < min)
                        min = perc[i];
               }
           return min;
    }
    
    float calc_maxper(float perc[6])
    {
           int i;
           float max;
           max = perc[0];
           
           for (i=1; i<6; i++)
               {
                     if (perc[i] > max)
                        max = perc[i];
               }
           return max;
    }
    Hope I get some ideas from all you.
    -Thanks in advance!!
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    your scanf function is not returnning anything. cause its wrong :)
    Code:
    scanf("d%", &nums); //wrong
    //it would be 
    scanf("%d", &nums);

    Comment

    • Metalman
      New Member
      • Apr 2012
      • 5

      #3
      Damn lol thanks man!!!

      It works ok now but it doesnt give full 100% if i add the percentage of all numbers. It goes up to like 95%-98%. Why is this happening?

      Also, any idea on how to make an histogram like this for my case?

      10%|*
      8% |* * *
      6% |* * * * *
      4% |* * * * * *
      2% |* * * * * *
      +------------
      1 2 3 4 5 6

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        calc_percentage () truncates the fractional percentage. That is, each of the six percentages is underestimated by error E where 0 <= E < 1. The sum all all six percentages thus ends up with error 6E, where 0 <= 6E < 6.

        Comment

        • Metalman
          New Member
          • Apr 2012
          • 5

          #5
          You are right man. Thanks a lot. Do you know what could possibly cause this? I mean it shouldnt do that since the variables are all float.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            The percentage variables are all float but the equation is all integer. Thus, the computation uses integer math to get an integer result which is then stored in a floating point variable.

            You can force the computation to use floating point math by insuring that at least one term is floating point. Change "100" to "100.".
            Last edited by donbock; Apr 20 '12, 11:56 AM. Reason: Removed my comment about calc_minper and calc_maxper. I was mistaken.

            Comment

            • Metalman
              New Member
              • Apr 2012
              • 5

              #7
              Yes man thank u for correcting me but thats the old code
              It isnt corrected or updated.
              Here is the (almost) final one:

              Code:
              #include <stdio.h> 
              #include <stdlib.h>
              #include <time.h>
              
              int random_number();
              float calc_percentage(int totals, int nums);
              float calc_maxper(float perc[6]);
              float calc_minper(float perc[6]);
              
              
              int main(void)
              {
                  int nums;
                  int i;
                  int totals[6] = {0};
                  float percentages[6] = {0};
                  float permin;
                  float permax;
              
                  srand(time(NULL));
                  
                  printf("How many numbers to generate?");
                  scanf("%d", &nums);
                  
              for (i = 1; i <= nums; i++)
              	{
              	    int x = random_number();
              	    totals[x-1]++;
              	}
              	
              for (i = 0; i<6; i++)
              {
                  percentages[i] = calc_percentage(totals[i],nums);
              }
              
              
              permin = calc_minper(percentages);
              permax = calc_maxper(percentages);
              
              if (((permax) - (permin)) > 5)
                 printf("The generator is not reliable.\n");
                 printf("The percentage difference is:%.1f\n\n", permax-permin);
                 
                 
                  printf("20%|");
                  for (i=0; i<6; i++)
                  {
                      if (percentages[i] >= 20)
                         printf("* ");
                  }
                  printf("\n");
                  
                  printf("16%|");
                  for (i=0; i<6; i++)
                  {
                      if (percentages[i] >= 16)
                         printf("* ");
                  }  
                  printf("\n");
                 
                  
                  printf("12%|");
                  for (i=0; i<6; i++)
                  {
                      if (percentages[i] >= 12)
                         printf("* ");
                  }  
                  printf("\n");
                  
                  
                  printf(" 8%|");
                  for (i=0; i<6; i++)
                  {
                      if (percentages[i] >= 8)
                         printf("* ");
                  }  
                  printf("\n");
                  
                  
                  printf(" 4%|");
                  for (i=0; i<6; i++)
                  {
                      if (percentages[i] >= 4)
                         printf("* ");
                  }  
               
                     
              
              printf("\n");
              printf("  +------------\n");   
              printf("   1 2 3 4 5 6\n");
                 
                 
                 
                 
              system("pause");
              return 0;
              }
              
              
              
              int random_number()
              {
                  int randnum;
                  randnum = 1 + (rand() % 6);
                  
                  return randnum;
              }
              
              float calc_percentage(int totals, int numbers)
              {
                    float a;
                    
                    a = (totals * 100)/numbers;
                    
                    return a;
              }
              
              float calc_minper(float perc[6])
              {
                     int i;
                     float min;
                     min = perc[0];
                     
                     for (i=1; i<6; i++)
                         {
                               if (perc[i] < min)
                                  min = perc[i];
                         }
                     return min;
              }
              
              float calc_maxper(float perc[6])
              {
                     int i;
                     float max;
                     max = perc[0];
                     
                     for (i=1; i<6; i++)
                         {
                               if (perc[i] > max)
                                  max = perc[i];
                         }
                     return max;
              }

              Comment

              • donbock
                Recognized Expert Top Contributor
                • Mar 2008
                • 2427

                #8
                The corrected and updated code still contains the same mistake in calc_percentage that I described in my previous post.

                Comment

                Working...