Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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;
- }
Add Comment
Please, Sign In to add comment