# Thread: probability of dice throw

1. ## probability of dice throw

I have wriiten the following code that generates a random dice throw 36000 times. It then prints out the number of times each pair is thrown. I want to also output the actual and expected probabilities fo each throw. Do I need another for loop to jump through the array, or what am i missing here. Any help would be greatly appreciated. Thanks to all the people who are smarter than the average bear.

code:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define SIZE 13 /*0-12*/

int main ()
{
int die_one, die_two, roll,j,sum, freq[SIZE] = {0};
float actual;

srand(time(NULL));

for (roll= 1; roll <= 36000; roll++)
{
die_one = rand () % 6 +1;
die_two = rand () % 6 +1;
sum = die_one + die_two;
++freq [sum];

}

printf("%s%10s%20s%22s\n", "Sum", "Freq", "Expected", "Actual");

for (sum =2; sum <= SIZE -1; sum++)
{
printf("%2d%10d%20f\n", sum, freq[sum], actual);

}
return 0;
}

Sum Freq Expected Actual
2 947
3 2034
4 2998
5 4063
6 5075
7 5889
8 4986
9 4005
10 2943
12 1020
Press any key to continue

2. > I want to also output the actual and expected probabilities fo each throw
Well the expected probablility is what you get from reading any statistics book.
You can pre-initialise an array with these values

Something like this, with numbers to suit
int expected[SIZE] = { 1, 2, 3, 4, 5, 6, 7 };

The actual results are all in freq[sum]

So your print statement would be
printf("%2d%10d%10d\n", sum, expected[sum], freq[sum] );

3. Your expected array is [1k, 2k, 3k, 4k, 5k, 6k, 5k, 4k, 3k, 2k, 1k]. Let us know if you need more info.

4. Hi,
How's everyone doing? I have a basic knowledge of C, therefore, I decided to participate in some questionaires regarding C. In the booklet I came across the following program. Although I have compiled this several times- Turbo C & DJGPP, I receive some errors. Let's have a look together..

/* Roll a six-sided dice 6000 times */

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

int main()
{

int face, roll, frequency1 = 0, frequency2 = 0,
frequency3 = 0, frequency4 = 0,
frequency5 = 0, frequency6 = 0;

for ( roll = 1; roll <= 6000; roll++ )
face = 1 + rand() % 6;

switch ( face )
{

case 1:
++frequency1;
break;
case 2:
++frequency2;
break;
case 3:
++frequency3;
break;
case 4:
++frequency4;
break;
case 5:
++frequency5;
break;
case 6:
++frequency6;
break;

}

}

printf( "%s%13s\n", "Face", "Frequency" );
printf( " 1%13d\n", frequency1 );
printf( " 2%13d\n", frequency2 );
printf( " 3%13d\n", frequency3 );
printf( " 4%13d\n", frequency4 );
printf( " 5%13d\n", frequency5 );
printf( " 6%13d\n", frequency6 );

return 0;

}

Could you compile this? Please let me know the result. I'd be thankful.

Al - IRAN..

5. you had too many brackets.

Code:
```/* Roll a six-sided dice 6000 times */

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

int main(void)
{
int face, roll, frequency1 = 0, frequency2 = 0,
frequency3 = 0, frequency4 = 0,
frequency5 = 0, frequency6 = 0;

for ( roll = 1; roll <= 6000; roll++ )
{
face = 1 + rand() % 6;

switch ( face )
{
case 1:
frequency1++;
break;
case 2:
frequency2++;
break;
case 3:
frequency3++;
break;
case 4:
frequency4++;
break;
case 5:
frequency5++;
break;
case 6:
frequency6++;
break;
}
}
printf( "%s%13s\n", "Face", "Frequency" );
printf( " 1%13d\n", frequency1 );
printf( " 2%13d\n", frequency2 );
printf( " 3%13d\n", frequency3 );
printf( " 4%13d\n", frequency4 );
printf( " 5%13d\n", frequency5 );
printf( " 6%13d\n", frequency6 );
return 0;
}```
edit: and information on getting better randomness is in the FAQ.