Guest User

Untitled

a guest
Jun 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <random>
  3.  
  4. float random_float() {
  5. static std::random_device rd;
  6. static std::default_random_engine re;
  7. static std::uniform_real_distribution<float> urd(0, 1);
  8.  
  9. return urd(re);
  10. }
  11.  
  12. float random_float(float max) {
  13. return random_float() * max;
  14. }
  15.  
  16. float random_float(float min, float max) {
  17. return random_float() * (max - min) + min;
  18. }
  19.  
  20. bool random_bool() {
  21. return random_float() < 0.5f;
  22. }
  23.  
  24. int main() {
  25. int iters = 10000000;
  26.  
  27. //chance: the percent chance that a male frog will croak once within a given time frame
  28. for (int chance = 5; chance <= 100; chance += 5) {
  29. int matches = 0;
  30. int lives1 = 0;
  31. int lives2 = 0;
  32.  
  33. for (int i = 0; i < iters; ++i) {
  34. //randomly pick gender of all frogs
  35. bool male0 = random_bool(); //side A
  36. bool male1 = random_bool(); //side B
  37. bool male2 = random_bool(); //side B
  38.  
  39. //determine if each frog croaks or not
  40. bool croak0 = male0? random_float(100) < chance : 0;
  41. bool croak1 = male1? random_float(100) < chance : 0;
  42. bool croak2 = male2? random_float(100) < chance : 0;
  43.  
  44. //eliminate situations that don't match the one supposed in the video
  45. //(no croak from side A, and exactly 1 croak from side B)
  46. if (croak0 || (croak1 && croak2) || (!croak1 && !croak2)) {
  47. continue;
  48. }
  49.  
  50. //tally up the stats
  51. matches += 1;
  52. if (!male0) {
  53. lives1 += 1;
  54. }
  55. if (!male1 || !male2) {
  56. lives2 += 1;
  57. }
  58. }
  59.  
  60. //human-readable output
  61. printf("frog croak chance: %3d%% scenario chance: %6.2f%% "
  62. "side A: %6.2f%% survival side B: %6.2f%% survival\n",
  63. chance, matches / (float)iters * 100,
  64. lives1 / (float)matches * 100, lives2 / (float)matches * 100);
  65.  
  66. //csv output (for them sick graphs, yo)
  67. // printf("%d,%f,%f,%f\n", chance, matches / (float)iters * 100,
  68. // lives1 / (float)matches * 100, lives2 / (float)matches * 100);
  69.  
  70. //makes the input appear line by line, to give a better sense of progress
  71. fflush(stdout);
  72. }
  73.  
  74. return 0;
  75. }
Add Comment
Please, Sign In to add comment