/* * spd * Mar 2003 * * Get a normal[] array of normally distributed random values. * Plot them and verify mean and variance (sigma2) * * We will chose random values from -RANGE/2 to RANGE/2 * For every value we will discard it according its gaussian probability * * Sample usage: ./normal 0 1000 16000 1 | plot ####################################################################### m: 0.297407 s: 1006.32 T: 16004241 total samples for 16000 used * */ #include #include #include #include #include #include #define sqr(a) ((a)*(a)) double f ( double x, 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 w) {fprintf(stderr,"#"); w=wn;} /* printf("%d %g\n",i, x);*/ } } fprintf(stderr,"\n"); /* normal[] should be a normal (m,s2) distribution */ if (plot) { for( i=0; i= 0.0 ) return( 1 - Fx ); else return Fx; } /************************************************************************** * * Uniform distribution */ double u ( double x, double min, double max ) { if (( x <= min ) || ( x >= max )) return 0; else return ( 1 / ( max - min ) ); }