# dice rolling in c

• 11-06-2010
ceddsoboss
dice rolling in c
Ok, I want to just say that this dice rolling program has been a pain in the .......... from the beginning, ok -- moving on.

I want to know if anyone at all could give me a hint as to how to find the average of multiple simulated rolls in a dice program. I know that a single roll has a 3.5 average and for two dice you multiply that times two... but what if I wanted to find the average of the sum for each of my 100000 random rolls?

Disclaimer: I know this is wrong, but....

This is what I thought of: 3.5 x 2 = 7 x 100000 = 700000 / 100000 = 7 ? ok, bare with me.

I also know that there is a 1/36 probability of rolling any number on the die. Maybe I should use this probability in the equation?

Code:

```printf("\n\nPlease input a number for the seed value: ");  //User needs to input a seed value           scanf("%f", &seed);                         srand( seed );                                                                                            //implementation of the random # generator function           for ( count = 0; count < 11; count++ )                                            // An array that makes sure the proper syncing of elements                                                                                                                         // takes place.                         rolls[ count ] = 0;             for ( count = 1; count <= 100000; count++ ) {                            //A counter that calls the 'roleDie' funtion                           twoDice = roleDie() + roleDie();                         ++ rolls[ twoDice - 2 ];  }    //printf("\nthis is twodice %d\n",twoDice);    //average = ( twoDice / 100000.0);         printf("\n\nThe number of times the simulated roll of dice was 2: %d\n\n", rolls[0]);           printf("The number of times the simulated roll of dice was 7: %d\n\n", rolls[5]);           printf("The nuber of times the simulated roll of dice was 12: %d\n\n", rolls[10]);           printf("The average of all simultaneous dice rolls is: %.3f", average);                   } //********************************* //  USER DEFINED FUNCTION: roleDie //    'Random Number Generator' //********************************* int roleDie() {  float result;  int  roll;    result = rand();  roll = (result * 6) / 32768;    return( roll + 1 ); }```
Ok guys, so you see the code. Can anyone clue me in?
• 11-06-2010
CommonTater
Ok a few things....

Your best random seed is time(0) ... srand(time(0));
Asking for user input is likely to result in many repititions of the same sequence. (Hint, most people will enter a single digit number)

The actual dice roll is not accurately represented in your rand function. You could simplify that down to simply say return (rand() % 6) + 1;

The average is pretty much the sum of all values from rolling the dice divided by the number of rolls... So you're on the right track by simply adding them up and dividing.
• 11-06-2010
ceddsoboss
Great, thanks commontater
• 11-06-2010