/* Generate random heights and weights, that are either */ /* correlated or not, depending on what you want. */ /* */ /* Written by Carey Williamson, University of Calgary */ /* */ /* Usage: cc -o heights heights.c */ /* ./heights */ #include #include #define NUMPEOPLE 100 /* Number of people to simulate */ /* Debugging output */ #define DEBUG 1 /***********************************************************************/ /* RANDOM NUMBER GENERATION STUFF */ /***********************************************************************/ /* Parameters for random number generation. */ #define MAX_INT 2147483647 /* Maximum positive integer 2^31 - 1 */ /* Generate a random floating point value uniformly distributed in [0,1] */ float Uniform01() { float randnum; /* get a random positive integer from random() */ randnum = (float) 1.0 * random(); /* divide by max int to get something in (0..1) */ randnum = (float) randnum / (1.0 * MAX_INT); return( randnum ); } /***********************************************************************/ /* MAIN PROGRAM */ /***********************************************************************/ int main() { int i; float val, val2; float heights[NUMPEOPLE], weights[NUMPEOPLE]; /* Initialization */ srandom(1234567); for( i = 0; i < NUMPEOPLE; i++ ) { val = Uniform01(); heights[i] = val * (200 - 120) + 120; /* Uniform(120,200) cm */ val2 = Uniform01(); if (val2 < 0.5 ) val = Uniform01(); weights[i] = val * (100 - 30) + 30; /* Uniform(30,100) kg */ #ifdef DEBUG printf("%4.1f %4.1f\n", heights[i], weights[i]); #endif } }