fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define ROLLS 10000
  6.  
  7. int main(void) {
  8. int frequency[13] = {0};
  9. int die1, die2, sum;
  10.  
  11. srand(time(NULL));
  12.  
  13. for (int i = 0; i < ROLLS; i++) {
  14. die1 = rand() % 6 + 1;
  15. die2 = rand() % 6 + 1;
  16. sum = die1 + die2;
  17. frequency[sum]++;
  18. }
  19.  
  20. // 結果を表示
  21. printf("出た目の和の頻度:\n");
  22. for (int i = 2; i <= 12; i++) {
  23. printf("%d: %d\n", i, frequency[i]);
  24. }
  25.  
  26. return 0;
  27. }
Success #stdin #stdout 0.01s 5276KB
stdin
Standard input is empty
stdout
出た目の和の頻度:
2: 268
3: 544
4: 807
5: 1142
6: 1419
7: 1681
8: 1384
9: 1125
10: 849
11: 525
12: 256