Created
October 30, 2013 04:21
-
-
Save jyc/7227168 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <stdio.h> | |
| #include <stdbool.h> | |
| #include <time.h> | |
| #define NUMBER_OF_TRIALS 10000000 | |
| bool both_eating(double time_juan, double time_han, double sample); | |
| double simulate(int number_of_trials); | |
| double xrand(); | |
| bool both_eating(double time_juan, double time_han, double sample) { | |
| return sample >= time_juan && sample < time_juan + 0.5 | |
| && sample >= time_han && sample < time_han + 0.5; | |
| } | |
| double simulate(int number_of_trials) { | |
| int tally = 0; | |
| int n; | |
| for (n = 0; n < number_of_trials; n++) { | |
| double time_juan = xrand() * 3; | |
| double time_han = xrand() * 3; | |
| if (both_eating(time_juan, time_han, xrand() * 3)) { | |
| tally++; | |
| } | |
| } | |
| return (double)tally / (double)number_of_trials; | |
| } | |
| double xrand() { | |
| return (double)rand() / (double)RAND_MAX; | |
| } | |
| int main(void) { | |
| srand(time(NULL)); | |
| printf("%f", simulate(NUMBER_OF_TRIALS)); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment