//=================================================== file = blockError.c ===== //= A Monte Carlo simulation of independent bit errors in a block = //= - For Assignment #3, problem #3 = //============================================================================= //= Notes: See problem #3 in assignment #3 = //=---------------------------------------------------------------------------= //= Build: bcc32 blockError.c = //=---------------------------------------------------------------------------= //= Execute: blockError = //=---------------------------------------------------------------------------= //= History: KJC (05/28/13) - Genesis = //============================================================================= //----- Include files --------------------------------------------------------- #include // Needed for printf() //----- Constants ------------------------------------------------------------- #define P_ERROR 0.01 // Probability of a bit error #define BLOCK_LEN 40 // Number of bits in a block #define NUM_TRIALS 10000000 // Number of trials to run //----- Function prototypes --------------------------------------------------- double rand_val(int seed); // RNG //===== Main program ========================================================== int main(void) { int error_count; // Error count for a message int error[BLOCK_LEN + 1]; // Error count vector int i, j; // Loop counters // Initialization error vector to zero for (i=0; i 0 for seeding = //========================================================================= double rand_val(int seed) { const long a = 16807; // Multiplier const long m = 2147483647; // Modulus const long q = 127773; // m div a const long r = 2836; // m mod a static long x; // Random int value (this is the seed) long x_div_q; // x divided by q long x_mod_q; // x modulo q long x_new; // New x value // Seed the RNG if (seed > 0) x = seed; // 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); }