Advertisement
BLUuuE

Coin Flip - Multithreaded Version

Oct 8th, 2015
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <limits>
  4. #include <cmath>
  5. #include <thread>
  6. using std::cout; using std::cin; using std::endl;
  7. using std::thread;
  8.  
  9. //flip the coin
  10. inline int flip()       //heads = 0, tails = 1
  11. {
  12.     return rand() % 2;
  13. }
  14.  
  15. //compares two arrays to see if they're equal
  16. template <size_t N, typename T>
  17. bool isSequenceEqual(T (&seq1)[N], T (&seq2)[N])
  18. {
  19.     int count = 0;
  20.     for (int i = 0; i < N; i++)
  21.     {
  22.         if (seq1[i] == seq2[i])
  23.             count++;
  24.     }
  25.     return count == N;      //if count is equal to N then the arrays are equal; else they aren't
  26. }
  27.  
  28. //function that returns number of coin flips needed to get desiredSeq[] -> nine heads in a row...
  29. unsigned long long Flips()
  30. {
  31.     const int FLIPS = 9;                //max amount of flips
  32.     int sequence[FLIPS] = { 0 };        //random sequence generated by flip()
  33.     int desiredSeq[FLIPS] = { 0 };      //the sequence we want; in this case, all heads
  34.     unsigned long long times = 0LL;     //amount of times the coin has been flipped
  35.  
  36.     do {
  37.         for (int flips = 0; flips < FLIPS; flips++)     //flip the coin 9 times...
  38.             sequence[flips] = flip();
  39.  
  40.         times++;
  41.     } while (!isSequenceEqual(sequence, desiredSeq));
  42.     return times;
  43. }
  44.  
  45. int avgFlips()
  46. {
  47.     typedef unsigned long long ull;
  48.     ull sum = 0;                //the accumulative total of the amount of times the coin has to be flipped to get nine heads...
  49.     double noOfFlips = 1e5;     //number of times to run the Flips() function; higher value = more accurate but longer to calculate...
  50.  
  51.     for (ull i = 0; i < noOfFlips; i++)
  52.         sum += Flips();
  53.  
  54.     cout << "The average number of trials needed to get 9 heads is " <<
  55.         round(sum / noOfFlips) << endl;     //round() from cmath; rounds the number...;
  56.     return round(sum / noOfFlips);
  57. }
  58.  
  59. int main()
  60. {
  61.     srand(time(NULL));      //seed rand(), so we get a "random" number from it...
  62.  
  63.     //change this to whatever you want; I have an i7 which has multithreading so I set it at 8...
  64.     const int numOfThreads = 8;
  65.     thread threads[numOfThreads];
  66.  
  67.     cout << "Creating threads...\n";
  68.     /*Create threads*/
  69.     for (int t = 0; t < numOfThreads; t++)
  70.         threads[t] = thread(avgFlips);
  71.     cout << "Threads created.\n" <<
  72.         "Running threads...\n\n";
  73.     /*Wait for threads to finish*/
  74.     for (int r = 0; r < numOfThreads; r++)
  75.         threads[r].join();
  76.  
  77.     cout << "Press enter to exit...";
  78.     getchar();
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement