Created
June 18, 2018 22:17
-
-
Save notnullnotvoid/cd813f39c078327c6dbdaa2dffb9a195 to your computer and use it in GitHub Desktop.
Monte Carlo sim of the inescapable, eternally relevant frog riddle.
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 <stdio.h> | |
#include <random> | |
float random_float() { | |
static std::random_device rd; | |
static std::default_random_engine re; | |
static std::uniform_real_distribution<float> urd(0, 1); | |
return urd(re); | |
} | |
float random_float(float max) { | |
return random_float() * max; | |
} | |
float random_float(float min, float max) { | |
return random_float() * (max - min) + min; | |
} | |
bool random_bool() { | |
return random_float() < 0.5f; | |
} | |
int main() { | |
int iters = 10000000; | |
//chance: the percent chance that a male frog will croak once within a given time frame | |
for (int chance = 5; chance <= 100; chance += 5) { | |
int matches = 0; | |
int lives1 = 0; | |
int lives2 = 0; | |
for (int i = 0; i < iters; ++i) { | |
//randomly pick gender of all frogs | |
bool male0 = random_bool(); //side A | |
bool male1 = random_bool(); //side B | |
bool male2 = random_bool(); //side B | |
//determine if each frog croaks or not | |
bool croak0 = male0? random_float(100) < chance : 0; | |
bool croak1 = male1? random_float(100) < chance : 0; | |
bool croak2 = male2? random_float(100) < chance : 0; | |
//eliminate situations that don't match the one supposed in the video | |
//(no croak from side A, and exactly 1 croak from side B) | |
if (croak0 || (croak1 && croak2) || (!croak1 && !croak2)) { | |
continue; | |
} | |
//tally up the stats | |
matches += 1; | |
if (!male0) { | |
lives1 += 1; | |
} | |
if (!male1 || !male2) { | |
lives2 += 1; | |
} | |
} | |
//human-readable output | |
printf("frog croak chance: %3d%% scenario chance: %6.2f%% " | |
"side A: %6.2f%% survival side B: %6.2f%% survival\n", | |
chance, matches / (float)iters * 100, | |
lives1 / (float)matches * 100, lives2 / (float)matches * 100); | |
//csv output (for them sick graphs, yo) | |
// printf("%d,%f,%f,%f\n", chance, matches / (float)iters * 100, | |
// lives1 / (float)matches * 100, lives2 / (float)matches * 100); | |
//makes the input appear line by line, to give a better sense of progress | |
fflush(stdout); | |
} | |
return 0; | |
} |
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
frog croak chance: 5% scenario chance: 4.76% side A: 51.34% survival side B: 51.26% survival | |
frog croak chance: 10% scenario chance: 9.03% side A: 52.60% survival side B: 52.63% survival | |
frog croak chance: 15% scenario chance: 12.83% side A: 54.13% survival side B: 54.07% survival | |
frog croak chance: 20% scenario chance: 16.20% side A: 55.54% survival side B: 55.56% survival | |
frog croak chance: 25% scenario chance: 19.14% side A: 57.17% survival side B: 57.12% survival | |
frog croak chance: 30% scenario chance: 21.68% side A: 58.81% survival side B: 58.84% survival | |
frog croak chance: 35% scenario chance: 23.82% side A: 60.61% survival side B: 60.61% survival | |
frog croak chance: 40% scenario chance: 25.62% side A: 62.52% survival side B: 62.53% survival | |
frog croak chance: 45% scenario chance: 27.04% side A: 64.51% survival side B: 64.51% survival | |
frog croak chance: 50% scenario chance: 28.12% side A: 66.68% survival side B: 66.67% survival | |
frog croak chance: 55% scenario chance: 28.91% side A: 68.97% survival side B: 68.97% survival | |
frog croak chance: 60% scenario chance: 29.40% side A: 71.47% survival side B: 71.39% survival | |
frog croak chance: 65% scenario chance: 29.60% side A: 74.05% survival side B: 74.07% survival | |
frog croak chance: 70% scenario chance: 29.57% side A: 76.90% survival side B: 76.89% survival | |
frog croak chance: 75% scenario chance: 29.28% side A: 79.95% survival side B: 79.98% survival | |
frog croak chance: 80% scenario chance: 28.81% side A: 83.35% survival side B: 83.34% survival | |
frog croak chance: 85% scenario chance: 28.10% side A: 86.96% survival side B: 86.97% survival | |
frog croak chance: 90% scenario chance: 27.24% side A: 90.92% survival side B: 90.88% survival | |
frog croak chance: 95% scenario chance: 26.18% side A: 95.24% survival side B: 95.21% survival | |
frog croak chance: 100% scenario chance: 24.97% side A: 100.00% survival side B: 100.00% survival |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment