擲骰子算機率

Run Settings
LanguageC
Language Version
Run Command
#include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { int d1, d2, sum_dice, times[13]; int i, run_times = 100; float rate; for (i = 0; i <= 12; i++) //初始陣列 times[i] = 0; srand((unsigned) time(NULL)); for(i = 1; i <= run_times; i++) { d1 = rand() % 6 + 1; d2 = rand() % 6 + 1; times[d1 + d2] = times[d1 + d2] + 1; printf("i=%4d 骰子=[%d][%d] 總點數=%d\n", i, d1, d2, d1 + d2); } for (i = 2; i <= 12; i++) { float run = run_times / 1.0; rate = times[i] / run * 100; printf("點數%2d 出現:%3d 機率:%5.1f%%\n", i, times[i], rate); } }
Editor Settings
Theme
Key bindings
Full width
Lines