Guest User

Untitled

a guest
Jan 24th, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. // INCLUDES
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. // DEFINES
  7. #define LOWER_DIE 1 // LOWER_DIE must be at least 1
  8. #define UPPER_DIE 10
  9. #define TRIALS 10000
  10.  
  11. // PROTOTYPES
  12. int genRand(int , int);
  13. int getTwoDieSum(int, int);
  14.  
  15. // MAIN
  16. int main() {
  17. int numberArray[2 * UPPER_DIE + 1]; // holds frequency of rolls
  18. int dieTotal = 0;
  19. int Die1, Die2 = 0;
  20.  
  21. // initialize numberArray
  22. int i = 0;
  23. for(int i = 0; i <= TRIALS; i++)
  24. numberArray[i] = 0;
  25.  
  26.  
  27.  
  28. // seed the random number generator
  29. srand((unsigned int)time(NULL));
  30.  
  31.  
  32. // roll the dice and keep track of what was rolled in the array
  33. for (i = 0; i <= TRIALS; i++){
  34. Die1 = genRand(LOWER_DIE, UPPER_DIE);
  35. Die2 = genRand(LOWER_DIE, UPPER_DIE);
  36. dieTotal = getTwoDieSum (Die1, Die2);
  37. numberArray[dieTotal]++;
  38. }
  39.  
  40. // display results
  41. i = 0;
  42. printf("After rolling the dice 10000 times, here are the results: /n");
  43. printf("\n");
  44. printf("ROLL VALUE | FREQUENCY");
  45. for (i = 0; i <= 10; i++)
  46. printf("%d | %d", i + 2, numberArray[i]);
  47.  
  48. return 0;
  49.  
  50. }
  51.  
  52.  
  53.  
  54. // FUNCTION IMPLEMENTATIONS
  55.  
  56. int genRand(int lower, int upper) {
  57. int range = (upper - lower) + 1;
  58. return rand() % range + lower;
  59. }
  60.  
  61. int getTwoDieSum(int roll1, int roll2) {
  62. int result = roll1 + roll2;
  63. return result;
  64. }
Add Comment
Please, Sign In to add comment