473,406 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.

Expand|Select|Wrap|Line Numbers
  1.  #include <stdio.h> 
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int random_number();
  6. float calc_percentage(int totals, int nums);
  7. float calc_maxper(float perc[6]);
  8. float calc_minper(float perc[6]);
  9. float permin;
  10. float permax;
  11.  
  12.  
  13. int main(void)
  14. {
  15.     int nums;
  16.     int i;
  17.     int totals[6] = {0};
  18.     float percentages[6] = {0};
  19.  
  20.     srand(time(NULL));
  21.  
  22.     printf("How many numbers to generate?");
  23.     scanf("d%", &nums);
  24.  
  25. for (i = 1; i <= nums; i++)
  26.     {
  27.         int x = random_number();
  28.         totals[x-1]++;   
  29.        printf("%d", x);
  30.     }
  31.  
  32. for (i = 0; i<6; i++)
  33. {
  34.     percentages[i] = calc_percentage(totals[i],nums);
  35.     printf("The percentage of each number is: %.2f%\n", percentages[i]);
  36. }
  37.  
  38.  
  39. permin = calc_minper(percentages);
  40. permax = calc_maxper(percentages);
  41.  
  42. if (((permax) - (permin)) > 5)
  43.    printf("According to the percentages of each number the generator is diss-functional\n");
  44.    printf("%.2f\n", permin);
  45.    printf("%.2f\n", permax);
  46.  
  47. system("pause");
  48. return 0;
  49. }
  50.  
  51.  
  52.  
  53. int random_number()
  54. {
  55.     int randnum;
  56.     randnum = 1 + (rand() % 6);
  57.  
  58.     return randnum;
  59. }
  60.  
  61. float calc_percentage(int totals, int numbers)
  62. {
  63.       float a;
  64.  
  65.       a = (totals * 100)/numbers;
  66.  
  67.       return a;
  68. }
  69.  
  70. float calc_minper(float perc[6])
  71. {
  72.        int i;
  73.        float min;
  74.        min = perc[0];
  75.  
  76.        for (i=1; i<6; i++)
  77.            {
  78.                  if (perc[i] < min)
  79.                     min = perc[i];
  80.            }
  81.        return min;
  82. }
  83.  
  84. float calc_maxper(float perc[6])
  85. {
  86.        int i;
  87.        float max;
  88.        max = perc[0];
  89.  
  90.        for (i=1; i<6; i++)
  91.            {
  92.                  if (perc[i] > max)
  93.                     max = perc[i];
  94.            }
  95.        return max;
  96. }
  97.  
Hope I get some ideas from all you.
-Thanks in advance!!
Apr 16 '12 #1

✓ answered by johny10151981

your scanf function is not returnning anything. cause its wrong :)
Expand|Select|Wrap|Line Numbers
  1. scanf("d%", &nums); //wrong
  2. //it would be 
  3. scanf("%d", &nums);
  4.  

7 5801
johny10151981
1,059 1GB
your scanf function is not returnning anything. cause its wrong :)
Expand|Select|Wrap|Line Numbers
  1. scanf("d%", &nums); //wrong
  2. //it would be 
  3. scanf("%d", &nums);
  4.  
Apr 16 '12 #2
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
Apr 16 '12 #3
donbock
2,426 Expert 2GB
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.
Apr 18 '12 #4
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.
Apr 18 '12 #5
donbock
2,426 Expert 2GB
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.".
Apr 18 '12 #6
Yes man thank u for correcting me but thats the old code
It isnt corrected or updated.
Here is the (almost) final one:

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h> 
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. int random_number();
  6. float calc_percentage(int totals, int nums);
  7. float calc_maxper(float perc[6]);
  8. float calc_minper(float perc[6]);
  9.  
  10.  
  11. int main(void)
  12. {
  13.     int nums;
  14.     int i;
  15.     int totals[6] = {0};
  16.     float percentages[6] = {0};
  17.     float permin;
  18.     float permax;
  19.  
  20.     srand(time(NULL));
  21.  
  22.     printf("How many numbers to generate?");
  23.     scanf("%d", &nums);
  24.  
  25. for (i = 1; i <= nums; i++)
  26.     {
  27.         int x = random_number();
  28.         totals[x-1]++;
  29.     }
  30.  
  31. for (i = 0; i<6; i++)
  32. {
  33.     percentages[i] = calc_percentage(totals[i],nums);
  34. }
  35.  
  36.  
  37. permin = calc_minper(percentages);
  38. permax = calc_maxper(percentages);
  39.  
  40. if (((permax) - (permin)) > 5)
  41.    printf("The generator is not reliable.\n");
  42.    printf("The percentage difference is:%.1f\n\n", permax-permin);
  43.  
  44.  
  45.     printf("20%|");
  46.     for (i=0; i<6; i++)
  47.     {
  48.         if (percentages[i] >= 20)
  49.            printf("* ");
  50.     }
  51.     printf("\n");
  52.  
  53.     printf("16%|");
  54.     for (i=0; i<6; i++)
  55.     {
  56.         if (percentages[i] >= 16)
  57.            printf("* ");
  58.     }  
  59.     printf("\n");
  60.  
  61.  
  62.     printf("12%|");
  63.     for (i=0; i<6; i++)
  64.     {
  65.         if (percentages[i] >= 12)
  66.            printf("* ");
  67.     }  
  68.     printf("\n");
  69.  
  70.  
  71.     printf(" 8%|");
  72.     for (i=0; i<6; i++)
  73.     {
  74.         if (percentages[i] >= 8)
  75.            printf("* ");
  76.     }  
  77.     printf("\n");
  78.  
  79.  
  80.     printf(" 4%|");
  81.     for (i=0; i<6; i++)
  82.     {
  83.         if (percentages[i] >= 4)
  84.            printf("* ");
  85.     }  
  86.  
  87.  
  88.  
  89. printf("\n");
  90. printf("  +------------\n");   
  91. printf("   1 2 3 4 5 6\n");
  92.  
  93.  
  94.  
  95.  
  96. system("pause");
  97. return 0;
  98. }
  99.  
  100.  
  101.  
  102. int random_number()
  103. {
  104.     int randnum;
  105.     randnum = 1 + (rand() % 6);
  106.  
  107.     return randnum;
  108. }
  109.  
  110. float calc_percentage(int totals, int numbers)
  111. {
  112.       float a;
  113.  
  114.       a = (totals * 100)/numbers;
  115.  
  116.       return a;
  117. }
  118.  
  119. float calc_minper(float perc[6])
  120. {
  121.        int i;
  122.        float min;
  123.        min = perc[0];
  124.  
  125.        for (i=1; i<6; i++)
  126.            {
  127.                  if (perc[i] < min)
  128.                     min = perc[i];
  129.            }
  130.        return min;
  131. }
  132.  
  133. float calc_maxper(float perc[6])
  134. {
  135.        int i;
  136.        float max;
  137.        max = perc[0];
  138.  
  139.        for (i=1; i<6; i++)
  140.            {
  141.                  if (perc[i] > max)
  142.                     max = perc[i];
  143.            }
  144.        return max;
  145. }
  146.  
Apr 18 '12 #7
donbock
2,426 Expert 2GB
The corrected and updated code still contains the same mistake in calc_percentage that I described in my previous post.
Apr 20 '12 #8

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

Similar topics

1
by: lawentzel | last post by:
I am new to PHP and am an avid RPG gamer. I am looking to use PhpMyChat to be able to game with friends all over the US. With that having been said, PhpMyChat seems to offer a lot of what I am...
101
by: Elijah Cardon | last post by:
Let's say I have m dice having n sides, such that n^m is not going to bust int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1, 2}. What is a good way to represent this in c so that...
17
by: Jose Durazo | last post by:
Hello, I'm doing an exercise to simulate rolling a pair of dice 36,000 times, then count and display how many times the simulation rolls each possible sum. For some reason each time I run my...
1
by: lenest | last post by:
I need help writing a program.... You are to write a python program to accomplish the following: a.. Play a dice game of Craps using a random number generator to simulate the roll of the...
38
by: d0ugg | last post by:
I'm writing a program that will have to roll the dice A and dice B one million times, and calculate the percentage of times that the dies will be equal. I'm having trouble to figure out how the...
4
by: clairelee0322 | last post by:
Hello guys again! I am a C++ beginner and I am working on another project.. It's called Dice Lab with Loops.. unforunately I don't even know how to start the program.... Don't blame me for not pay...
2
by: sunnydude | last post by:
Anybody know how to write a 3 dice rolling program like this Sample output Welcome to the dice roller! First of all, please enter a seed for the random number generator. This should be a...
1
by: clairelee0322 | last post by:
I am a c++ beginner and i am working on a dice loop lab. This program should roll two dice (each dice is from 1 to 6) and then total them together. The user should be able to input how many times...
4
by: alexben | last post by:
Please I need help with this. Write a C++ program to roll 3 dice, show the values of these dice, and show the total value of these dice.? I actually did this but I am very sure it is wrong. ...
3
by: matt murray | last post by:
I'm writing a text based adventure game and was wondering if there was a way to "Roll Dice". An example of what i am trying to use this for would be attacking or defending in the game. If they get...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.