擲骰子的問題
請問各位大大幾個程式的問題
使用亂數產生一長度為n,範圍介於1~6的亂數陣列,並將該陣列以函數参數的方式回傳,來模擬擲了n次骰子的結果。
#include "stdio.h"
#include "stdlib.h"
// dist[6] : given distribution of each number's appearances
// result[n] : simulation result
// n : rolling a dice n times
//
void rolling(int dist[6], int result[][2], int n) {
//在這加入程式碼 }
void print(int vals[][2], int len) {
int i;
int cnt[6];
for(i=0; i<6; i++)
cnt[i]=0;
printf("Rolling result : ");
for(i=0; i<len; i++) {
printf("(%d,%d) ",vals[i][0],vals[i][1]);
int index=vals[i][0]-1;
cnt[index]++;
index=vals[i][1]-1;
cnt[index]++;
}
printf("\n");
for(i=0; i<6; i++) {
printf("\tdice(%d) = %4.1f \n",i+1,(double)cnt[i]/len*100);
}
printf("\n");
}
int main(){
const int n=100;
int dices[n][2];
int dist1[] = {3,3,3,6,6,12};
int dist2[] = {6,3,6,1,1,12};
int dist3[] = {1,1,2,1,1,4};
srand(time(NULL));
rolling(dist1,dices,n);
print(dices,n);
rolling(dist2,dices,n);
print(dices,n);
rolling(dist3,dices,n);
print(dices,n);
return 0;
}
|