//================================================== file = ballsBinsSim.c ===== //= Simulation of N balls thrown into M bins = //============================================================================== //= Notes: = //= 1) Need to set NUM_EXPER, M, and N in #define section = //= 2) Determines Pr[i bins are non-empty] = //=----------------------------------------------------------------------------= //= Example execution: = //= = //= Running... = //= =========================================== ballsBinsSim.c ===== = //= = Number of experiments = 10000000 = //= = M = 4 bins = //= = N = 8 balls = //= =--------------------------------------------------------------- = //= = Pr[ 1 bins are non-empty] = 0.000063 = //= = Pr[ 2 bins are non-empty] = 0.023373 = //= = Pr[ 3 bins are non-empty] = 0.353744 = //= = Pr[ 4 bins are non-empty] = 0.622819 = //= ================================================================ = //=----------------------------------------------------------------------------= //= Build: bcc32 ballsBinsSim.c = //=----------------------------------------------------------------------------= //= Execute: ballsBinsSim.c = //=----------------------------------------------------------------------------= //= Author: Ken Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@cse.usf.edu = //=----------------------------------------------------------------------------= //= History: KJC (04/11/09) - Genesis (from bfSim.c) = //============================================================================== //----- Include files ---------------------------------------------------------- #include // Needed for printf() #include // Needed for pow() //----- Constant defines ------------------------------------------------------- #define NUM_EXPER 10000000 // Number of experiments to run #define M 3 // Number of bins #define N 3 // Number of balls //----- Function prototypes ---------------------------------------------------- unsigned int randInt(int seed); // LCG from Jain //============================================================================== //= Main program = //============================================================================== void main(void) { unsigned int bin[M]; // The bins unsigned int nonEmptyCount[M+1]; // Count of occupied bins unsigned int count; // Counter of occupied bins unsigned int index; // Bin index (0, 1, ... M) int i,j; // Loop counters // Seed the RNG randInt(1); // Clear binCount vector for (i=1; i<=M; i++) nonEmptyCount[i] = 0; // Loop for number of experiments to run printf("Running... \n"); for (i=0; i 0) x = x_new; else x = x_new + m; // Return a random unsigned integer return(x); }