// Arup Guha
// 9/21/2011
// Written in COP 3223H - a simulation that figures out when
// you have to leave home to get a less than 2% chance of being late.
#include <stdio.h>
#include <time.h>

const int NUMTRIALS = 1000000;
const double LATETOLERANCE = 2;

int main() {

    srand(time(0));

    int timebefore = 30;
    double latepercentage = 100;

    // Loop until our we are late less than the tolerance indicates.
    while (latepercentage > LATETOLERANCE) {

        // Initialize our variables.
        int times_late = 0;
        timebefore++;

        // Run our trials.
        int i;
        for (i=0; i<NUMTRIALS; i++) {

            // It takes me 25 to 40 minutes to get to work.
            int mytime = rand()%16 + 25;

            // My boss checks on me in between 30 and 60 minutes after 8:30am.
            int bosstime = timebefore + rand()%31;

            // This means I was late, because my boss checked on me and I wasn't there.
            if (mytime > bosstime)
               times_late++;
        }

        // Calculate the percentage for this trial and print.
        latepercentage = (double)times_late/NUMTRIALS*100;
        printf("%lf %d\n", latepercentage, timebefore);
    }

    // Print the output for achieving the tolerance.
    printf("I will only be late %lf percent of the time if I start %d minutes before the boss.\n", latepercentage, timebefore);

    return 0;
}
