//=================================================== file = lotterySim.c ===== //= A Monte Carlo simulation of a lottery = //============================================================================= //= Notes: = //= 1) Need to set M and N in #defines = //= 1) Need to initialize N digits (all unique and from 1 to M) = //=---------------------------------------------------------------------------= //= Build: bcc32 lotterySim.c = //=---------------------------------------------------------------------------= //= Execute: lotterySim = //=---------------------------------------------------------------------------= //= History: KJC (06/02/09) - Genesis (from baby.c) = //============================================================================= //----- Include files --------------------------------------------------------- #include // Needed for printf() #include // Needed for rand() //----- Constants ------------------------------------------------------------- #define M 10 // Number of balls in bucket #define N 3 // Number of balls to select #define NUM_PLAYS 10000000 // Number of plays to run //----- Function prototypes --------------------------------------------------- void shuffle(int *X, int numElements); // Shuffling function unsigned int randInt(unsigned int seed); // RNG //===== Main program ========================================================== int main(void) { int ball[M]; // Balls put in bin and shuffled int num[N]; // Digits picked int selectedBall[N]; // Selected balls int matchCount; // Digit match counter int winCount; // Win counter (selected digits match balls pulled) int i, j, k; // Loop indexes // Initialize with N digits picked (all unique and from 1 to M) num[0] = 1; num[1] = 2; num[2] = 3; // Initialize the RNG randInt(1); // Main simulation loop printf("BEGIN... \n"); winCount = 0; for (i=0; i 0 to seed the RNG, seed == 0 to get RNG values = //========================================================================= unsigned int randInt(unsigned 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 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(x); }