//==================================================== file = gennorm.c ===== //= Program to generate nomrally distributed random variables = //=========================================================================== //= Notes: 1) Writes to a user specified output file = //= 2) Generates user specified number of samples = //= 3) Uses the Box-Muller method and only generates one of two = //= paired normal random variables. = //=-------------------------------------------------------------------------= //= Example user input: = //= = //= ---------------------------------------- gennorm.c ----- = //= - Program to generate normally distributed random - = //= - variables - = //= -------------------------------------------------------- = //= Output file name ===================================> output.dat = //= Random number seed =================================> 1 = //= Mean ===============================================> 0 = //= Standard deviation =================================> 1 = //= Number of samples to generate ======================> 10 = //= -------------------------------------------------------- = //= - Generating samples to file - = //= -------------------------------------------------------- = //= -------------------------------------------------------- = //= - Done! = //= -------------------------------------------------------- = //=-------------------------------------------------------------------------= //= Example output (from above user input): = //= = //= 3.015928 = //= 1.446444 = //= 0.294214 = //= 0.372630 = //= 0.802585 = //= -1.509856 = //= -0.672829 = //= 1.033490 = //= 0.759008 = //= 0.078499 = //=-------------------------------------------------------------------------= //= Build: bcc32 gennorm.c = //=-------------------------------------------------------------------------= //= Execute: gennorm = //=-------------------------------------------------------------------------= //= Author: Kenneth J. Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: KJC (06/06/02) - Genesis = //= KJC (05/20/03) - Added Jain's RNG for finer granularity = //=========================================================================== //----- Include files ------------------------------------------------------- #include // Needed for printf() #include // Needed for exit() and ato*() #include // Needed for sqrt() and log() //----- Defines ------------------------------------------------------------- #define PI 3.14159265 // The value of pi //----- Function prototypes ------------------------------------------------- double norm(double mean, double std_dev); // Returns a normal rv double rand_val(int seed); // Jain's RNG //===== Main program ======================================================== void main(void) { FILE *fp_out; // File pointer to output file char instring[80]; // Input string double num_samples; // Number of samples to generate double mean, std_dev; // Mean and standard deviation double norm_rv; // The adjusted normal rv int i; // Loop counter // Output banner printf("---------------------------------------- gennorm.c ----- \n"); printf("- Program to generate normally distributed random - \n"); printf("- variables - \n"); printf("-------------------------------------------------------- \n"); // Prompt for output filename and then create/open the file printf("Output file name ===================================> "); scanf("%s", instring); fp_out = fopen(instring, "w"); if (fp_out == NULL) { printf("ERROR in creating output file (%s) \n", instring); exit(1); } // Prompt for random number seed and then use it printf("Random number seed =================================> "); scanf("%s", instring); rand_val((int) atoi(instring)); // Prompt for mean value printf("Mean ===============================================> "); scanf("%s", instring); mean = atof(instring); // Prompt for standard deviation printf("Standard deviation =================================> "); scanf("%s", instring); std_dev = atof(instring); // Prompt for number of samples to generate printf("Number of samples to generate ======================> "); scanf("%s", instring); num_samples = atoi(instring); // Output message and generate interarrival times printf("-------------------------------------------------------- \n"); printf("- Generating samples to file - \n"); printf("-------------------------------------------------------- \n"); for (i=0; i 0) { x = seed; return(0.0); } // RNG using integer arithmetic x_div_q = x / q; x_mod_q = x % q; x_new = (a * x_mod_q) - (r * x_div_q); if (x_new > 0) x = x_new; else x = x_new + m; // Return a random value between 0.0 and 1.0 return((double) x / m); }