Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // INCLUDES
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- // DEFINES
- #define LOWER_DIE 1 // LOWER_DIE must be at least 1
- #define UPPER_DIE 10
- #define TRIALS 10000
- // PROTOTYPES
- int genRand(int , int);
- int getTwoDieSum(int, int);
- // MAIN
- int main() {
- int numberArray[2 * UPPER_DIE + 1]; // holds frequency of rolls
- int dieTotal = 0;
- int Die1, Die2 = 0;
- // initialize numberArray
- int i = 0;
- for(int i = 0; i <= TRIALS; i++)
- numberArray[i] = 0;
- // seed the random number generator
- srand((unsigned int)time(NULL));
- // roll the dice and keep track of what was rolled in the array
- for (i = 0; i <= TRIALS; i++){
- Die1 = genRand(LOWER_DIE, UPPER_DIE);
- Die2 = genRand(LOWER_DIE, UPPER_DIE);
- dieTotal = getTwoDieSum (Die1, Die2);
- numberArray[dieTotal]++;
- }
- // display results
- i = 0;
- printf("After rolling the dice 10000 times, here are the results: /n");
- printf("\n");
- printf("ROLL VALUE | FREQUENCY");
- for (i = 0; i <= 10; i++)
- printf("%d | %d", i + 2, numberArray[i]);
- return 0;
- }
- // FUNCTION IMPLEMENTATIONS
- int genRand(int lower, int upper) {
- int range = (upper - lower) + 1;
- return rand() % range + lower;
- }
- int getTwoDieSum(int roll1, int roll2) {
- int result = roll1 + roll2;
- return result;
- }
Add Comment
Please, Sign In to add comment