/* * Test for simple generation of distribution and sort. * * Telford Tendys, Copyright (C) 2017 * * This program may be re-distributed without modification. */ #include #include #include #include #define NUM_PEOPLE 1000000 double cq[ NUM_PEOPLE ]; double Box_Muller( void ) { for(;;) { double bm; double u = random(); double v = random(); u /= RAND_MAX; v /= RAND_MAX; bm = sqrt( -2 * log( u )) * cos( 2 * M_PI * v ); /* * Note that standard normal has mean of 0 and SD of 1, * but Psychologist normal is mean 100, and SD of 15. * Also for no particular reason, reject the possibility * of anything being below 0 nor greater than 200. */ bm *= 15; bm += 100; if( bm >= 0 && bm <= 200 ) { return( bm ); } } } int cq_compar( const void *p1, const void *p2 ) { register const double *dp1; register const double *dp2; dp1 = p1; dp2 = p2; if( *dp1 > *dp2 ) { return( 1 ); } if( *dp1 < *dp2 ) { return( -1 ); } /* Not expecting any NaN, Inf, etc */ return( 0 ); } /* * Very simple array fill with normal random numbers. * Then sort the array from lowest to highest. */ int fill_cq() { int i; /* * Put a bunch of normally distributed random numbers together */ for( i = 0; i < NUM_PEOPLE; i++ ) { cq[ i ] = Box_Muller(); } /* * Sort the stack (standard library sort function) */ qsort( cq, NUM_PEOPLE, sizeof( cq[ 0 ]), &cq_compar ); return( 0 ); } int main( void ) { int i, refil; FILE *fp; srand( 123456 ); fill_cq(); /* Dump out a basic set of CQ numbers to show population */ fp = fopen( "population_cq.dat", "w" ); fprintf( fp, "CQ\n" ); for( i = 0; i < NUM_PEOPLE; i++ ) { fprintf( fp, "%f\n", cq[ i ]); } fclose( fp ); return( 0 ); }