#include "stdlib.h" float mean(int n, float sample[]) //算平均 { float sum = 0.0; for(int i=0; i<n; i++) sum += sample[i]; return(sum/n); } float var(int n, float sample[]) //標準差 { float sum = 0.0; float u = mean(n, sample); for(int i=0; i<n; i++) sum += (sample[i]-u) * (sample[i]-u); return(sum/n); } float u_rand() // 0 ~ 1 { return (float)rand()/RAND_MAX; } float n_rand() // 平均 0 { float sum = 0.0; for(int i=0; i<12; i++) sum += u_rand(); return(sum-6); } float t_rand(int n, float u, float s) //漁夫 n->數量 { float *sample; sample = new float[n]; for(int i=0; i<n; i++) { sample[i] = n_rand() * s + u; } return(mean(n, sample)); } float f_rand(int k, int n,float u, float s) //可信度 u->平均值 s->標準差 { float *sample; float *group_mean; sample = new float [k*n]; group_mean = new float [k]; for(int i=0; i<k; i++) { for(int j=0; j<n; j++) sample[i*n+j ] = n_rand()*s+u; group_mean[i] = mean(n, sample + i*n); } float total_var = var(k, group_mean); delete group_mean; delete sample; return(total_var); }
全站熱搜
留言列表