Take the 2-minute tour ×

Would you mind helping me to solving the following question? Thanks.

Question: The percentage of rolling 3 dices of face 6 in one go can be found out mathematically or simulation. The Monte Carlo method is a computer process to find out the solution of a problem by computer simulation. Write a program that roll three dices, calculate their sum, and find out the probability of rolling each possible outcome.

You are given a skeleton program q1dskeleton.c that generates the statistics of rolling a dice of six sides 10000 times. Modify the program so that it generates the statistics of the sum of rolling three six-sided dices. An example of the program output is given in the following. Note that because of the random nature of dice rolling.

Hint: Call rand() three times for every throw of the dices, one for each dice.


#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define SIZE 7

int main() {

  int face, roll, frequency[SIZE] = { 0 };

  srand( time(NULL) );

  for (roll = 1; roll <= 10000; roll++) {
    face = rand() % 6 + 1;
  printf("%s%12s\n", "Face", "Frequency");

  for (face = 1; face <= SIZE - 1; face++)
    printf("%4d%12d\n", face, frequency[face]);


Face Frequency
3     49
4     129
5     276
6     481
7     669
8     994
9     1131
10    1213
11    1269
12    1197
13    962
14    707
15    464
16    268
17    144
18    47
share|improve this question
Is that code yours or given by the question? If given, have you tried anything? –  BoltClock Mar 8 '11 at 7:50
@Ilya Kogan: free or not, I wouldn't want to do the OP's homework just because it won't help him to become a programmer. –  ereOn Mar 8 '11 at 7:53
The only questions I see are "can anyone help?" and "would you mind helping me?"-- the answers are yes and yes. –  Beta Mar 8 '11 at 7:55
This has to be the easiest question I've ever seen posted here, and with the least effort put in before asking. –  justaname Mar 8 '11 at 7:56
See the hint that you posted. Then find out the rand() function in the code. Try to understand what the code is doing there, then you'll be able to solve the problem easily –  s_b Mar 8 '11 at 7:57
show 2 more comments

2 Answers

up vote 2 down vote accepted

Also, when calculating rand() you need to realize it is not all that random as we hope.

From the documentation at: http://www.cplusplus.com/reference/clibrary/cstdlib/rand/ "Notice though that this modulo operation does not generate a truly uniformly distributed random number in the span (since in most cases lower numbers are slightly more likely), but it is generally a good approximation for short spans."

So to get better results it is best to do some work on getting a different formula for a rand range that has a higher probability of random results.

For instance, for dice one you get a random number between 30 and 90 and the formula is (int)(((float)result - 30) / 10) + 0.5 or something along those lines.

Then for dice 2 you could get a number between 500 and 50000 and make another formula.

To make your homework assignment more interesting you could have a look at how different online casinos/poker rooms do this. http://www.fulltiltpoker.com/random-number-generator

share|improve this answer
Thank you very much for your help. –  Terence Mar 8 '11 at 8:34
add comment

Obviously, you have to increase SIZE since there are no longer 6 possibilities, but 18 - I'll include the impossible totals of 1 and 2 here since your original solution included the impossible value of zero :-)

Then, instead of throwing one dice, you throw three and then add up the values. So something like (pseudo-code):

dice1 = first random number
dice2 = second random number
dice3 = third random number
face = dice1 plus dice2 plus dice3

That addition is what you then use to increase a specific array element.

For bonus points, you should probably avoid printing out the impossible outcomes. That's a fairly simple modification to the starting bit of your final for statement.

The ratios you expect, by the way, are {1, 3, 6, 10, 15, 21, 25, 27, 27, 25, 21, 15, 10, 6, 3, 1} so the "ideal" output should be reasonably close to:

Face   Frequency
   3          46
   4         138
   5         277
   6         462
   7         694
   8         972
   9        1157
  10        1250
  11        1250
  12        1157
  13         972
  14         694
  15         462
  16         277
  17         138
  18          46

(even though that only adds up to 9992 samples - it's a crapshoot as to where the other eight show up).

share|improve this answer
add comment

Your Answer


