#include "stdio.h"
#include "stdlib.h"
#define random(num) (rand() % (num)) /* random 取亂數 */
#define randomize() srand((unsigned)time(NULL)) /* randomize 清除取亂數種子表 */
#define clrscr() system("CLS") /* 畫面清除 */
#define pause() system("PAUSE") /* 暫停畫面 */
/*
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) {
int i,c;
randomize();
for(i=0;i<n;i++)
{
c=random(6);
result[i][0]=i+1;
result[i][1]=dist[c];
}
}
void print(int dist[6],int vals[][2], int len) {
int i;
int cnt[6]={0};
int index;
/* for(i=0; i<6; i++) */
/* cnt[i]=0; */
clrscr();
printf("Rolling result : ");
for(i=0;i<6;i++)
printf("(%d) ",dist[i]);
printf("\n");
for(i=0; i<len; i++) {
printf("(%3d,%2d) ",vals[i][0],vals[i][1]);
/* int index=vals[i][0]-1; */
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");
pause();
}
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(dist1,dices,n);
rolling(dist2,dices,n);
print(dist2,dices,n);
rolling(dist3,dices,n);
print(dist3,dices,n);
}
|