fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int roll_dice(void) {
  5. return (rand() % 6) + 1; // returns a value between 1 and 6
  6. }
  7.  
  8. int main(void) {
  9. int sum, i;
  10. int frequency[13] = {0}; // Array to store the frequency of sums (2-12)
  11.  
  12. // Simulate rolling two dice 100,000 times
  13. for (i = 0; i < 100000; i++) {
  14. int die1 = roll_dice();
  15. int die2 = roll_dice();
  16. sum = die1 + die2;
  17. frequency[sum]++;
  18. }
  19.  
  20. // Output the results
  21. printf("Sum\tFrequency\n");
  22. for (i = 2; i <= 12; i++) {
  23. printf("%d\t%d\n", i, frequency[i]);
  24. }
  25.  
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
Sum	Frequency
2	2760
3	5546
4	8388
5	11209
6	13914
7	16786
8	13672
9	11106
10	8439
11	5462
12	2718