static bool randomChanceSeemsReasonable(double p) { int i, hits; double expected; hits = 0; for (i = 0; i < N_TRIALS; i++) { if (randomChance(p)) hits++; } expected = p * N_TRIALS; if (hits < 0.5 * expected || (p == 1.0 && hits != N_TRIALS)) { reportError("Hit count low"); return false; } else if (hits > 1.5 * expected || (p == 0.0 && hits != 0)) { reportError("Hit count high"); return false; } return true; }
/* * Function: RunSimulation * Usage: RunSimulation(); * ----------------------- * This function runs the actual simulation. In each time unit, * the program first checks to see whether a new customer arrives. * Then, if the cashier is busy (indicated by a nonzero value for * serviceTimeRemaining), the program decrements that variable to * indicate that one more time unit has passed. Finally, if the * cashier is free, the simulation serves another customer from * the queue after recording the waiting time for that customer. */ void runSimulation() { Queue<int> queue; int serviceTimeRemaining = 0; int nServed = 0; long totalWait = 0; long totalLength = 0; for (int t = 0; t < SIMULATION_TIME; t++) { if (randomChance(ARRIVAL_PROBABILITY)) { queue.enqueue(t); } if (serviceTimeRemaining > 0) { serviceTimeRemaining--; if (serviceTimeRemaining == 0) nServed++; } else if (!queue.isEmpty()) { totalWait += t - queue.dequeue(); serviceTimeRemaining = randomInteger(MIN_SERVICE_TIME, MAX_SERVICE_TIME); } totalLength += queue.size(); } reportResults(nServed, totalWait, totalLength); }