//======================================================== file = lec5b.c ===== //= Program to generate simulated page counts for print jobs = //============================================================================= //= Build: bcc32 lec5b.c, cl lec5b.c, gcc lec5b.c = //=---------------------------------------------------------------------------= //= Execute: lec5b = //=---------------------------------------------------------------------------= //= History: KJC (05/30/13) - Refresh from old version = //============================================================================= #include // Needed for printf() #include // Needed for rand() and RAND_MAX //----- Constants ------------------------------------------------------------- #define NUM_MEAS 10 // Number of measurements in "bins" #define NUM_SAMPLES 20 // Number of print job samples to simulate //----- Globals --------------------------------------------------------------- struct print_job // Structure for holding print job samples { int print_size; // ** Page count for this print job double ratio_observed; // ** Observed ratio (0 to 1) for this sample }; // Observed print job samples where sum of ratios must equal one struct print_job size[NUM_MEAS] = { {2, 0.22}, {3, 0.31}, {4, 0.16}, {5, 0.09}, {6, 0.07}, {7, 0.05}, {8, 0.04}, {9, 0.03}, {10, 0.02}, {11, 0.01} }; //===== Main program ========================================================== void main(void) { double dist_func[NUM_MEAS]; // The CDF for the print job samples double z; // Uniform random number from 0 to 1 int bin_num; // Measurement "bin" number int page_count; // Final page count for this sample int i; // Loop counter // <#1> Build the CDF for the observed measurements dist_func[0] = size[0].ratio_observed; for (i=1; i Output the CDF values for (i=0; i Generate NUM_SAMPLES print jobs for (i=0; i Pull a uniform RV (0 < z < 1) do { z = ((double) rand() / RAND_MAX); } while ((z == 0) || (z == 1)); // <#3b> Map z to a bin number for (bin_num=0; bin_num Get page_count as print_size from selected bin page_count = size[bin_num].print_size; break; } // <#3d> Output simulated page count printf("Sample #%d -- page count = %d \n", i, page_count); } }