Advertisement
Guest User

Untitled

a guest
Jun 20th, 2016
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.84 KB | None | 0 0
  1. #include <random>
  2. #include <iostream>
  3.  
  4. std::random_device rd;
  5. std::uniform_int_distribution<int> distribution(0, 1);
  6. std::mt19937 engine(rd()); // Mersenne twister MT19937
  7.  
  8. bool coin ()
  9. {
  10.     return distribution (engine);
  11. }
  12.  
  13. int main ()
  14. {
  15.     int a = 1000;
  16.     int b = 0;
  17.  
  18.     for (int i = 0; i < a; ++i)
  19.         b += (coin () ? 1 : 0);
  20.  
  21.     std :: cout << "Verify coin(): " << b << "/" << a << "\n\n";
  22.  
  23.     for (long int TRIALS = 10; TRIALS < 1000000000000; TRIALS *= 10)
  24.     {
  25.         long double winnings = 0;
  26.  
  27.         for (int i = 0; i < TRIALS; ++i)
  28.         {
  29.             long double pot = 2;
  30.  
  31.             if (! coin ())
  32.                 continue;
  33.  
  34.             while (true)
  35.             {
  36.                 if (! coin ())
  37.                     break;
  38.  
  39.                 pot *= 2;
  40.             }
  41.  
  42.             // std :: cout << "Won $" << pot << "\n";
  43.  
  44.             winnings += pot;
  45.         }
  46.  
  47.         std :: cout << TRIALS << ",\t" << (winnings / TRIALS) << "\n"
  48.             << std :: flush;
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement