/* * spd * May 2006 * * Get a normal[] array of normally distributed random values. * Plot them and verify mean and variance (sigma2) * * Sample usage: ./normal_aprox 0 1000 16000 1 | plot ####################################################################### m: -0.208904 s: 1004.8 T: 16000 total samples for 16000 used * */ #include #include #include #include #include #include #define sqr(a) ((a)*(a)) double NormalDist(double m, double s2); #define DIST_INTERVALS 400 #define SCREEN_W 70 int dist[DIST_INTERVALS]; #define RANGE 1000.0 int main( int argc, char *argv[] ) { int total=0, i, w=-1, wn; double s2, m, x, sum, med; struct timeval tp; int plot; double *normal; int normal_samples; m = (double) atof( argv[1] ); s2 = (double) atof( argv[2] ); normal_samples = (int) atoi( argv[3] ); plot = (int) atoi( argv[4] ); if(NULL==(normal=malloc(normal_samples*sizeof(double)))) { perror("malloc"); exit(1); } #if defined _POSIX_VERSION || defined _HPUX_SOURCE gettimeofday(&tp, NULL); #else gettimeofday(&tp); #endif /* drand48 return values uniformly distributed [0.0,~1.0) */ srand48((long)tp.tv_usec); for( i = 0; i= 0.0 ) && (y