Advertisement
FractalFusion

Randomness

Jan 6th, 2017
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. //Based on refutation to given solution to Riddler Express at http://fivethirtyeight.com/features/dont-throw-out-that-calendar/#ss-1
  2.  
  3. //sample output:
  4. /*
  5. Games played: 2000000
  6. Games won: 32271
  7. Proportion: 0.0161355
  8. Games played: 2000000
  9. Games won: 32377
  10. Proportion: 0.0161885
  11. Games played: 2000000
  12. Games won: 32272
  13. Proportion: 0.016136
  14. Games played: 2000000
  15. Games won: 32450
  16. Proportion: 0.016225
  17. Games played: 2000000
  18. Games won: 32500
  19. Proportion: 0.01625
  20. Games played: 2000000
  21. Games won: 32304
  22. Proportion: 0.016152
  23. Games played: 2000000
  24. Games won: 32554
  25. Proportion: 0.016277
  26. Games played: 2000000
  27. Games won: 32548
  28. Proportion: 0.016274
  29. Games played: 2000000
  30. Games won: 32682
  31. Proportion: 0.016341
  32. Games played: 2000000
  33. Games won: 32466
  34. Proportion: 0.016233
  35. Total games played: 20000000
  36. Total games won: 324424
  37. Proportion: 0.0162212
  38. */
  39.  
  40. #include <random>
  41. #include <iostream>
  42. #include <sstream>
  43. #include <chrono>
  44.  
  45. #define LIMIT 2000000
  46. #define REPEATS 10
  47.  
  48. //std::random_device rd;
  49. unsigned seed = static_cast<int> (std::chrono::system_clock::now().time_since_epoch().count());
  50.  
  51.  
  52. std::mt19937 rval(seed);
  53.  
  54. int main(int argc, char** argv)
  55. {
  56.     int deck[52];
  57.    
  58.    
  59.     int totalwins=0;
  60.     for(int kk=0;kk<REPEATS;kk++)
  61.     {
  62.        
  63.         int countwin=0;
  64.        
  65.         for(int k=0;k<LIMIT;k++)
  66.         {
  67.        
  68.             for(int i=0;i<52;i++) deck[i]=-1;
  69.            
  70.             for(int i=51;i>=0;i--)
  71.             {
  72.                 unsigned int j=rval()%(i+1);
  73.                
  74.                 int pos=0;
  75.                 while(j>0 || deck[pos]>=0)
  76.                 {
  77.                     if(deck[pos]>=0) pos++;
  78.                     else {pos++; j--;}
  79.                 }
  80.                 deck[pos]=i;
  81.                
  82.             }
  83.            
  84.             //flip over card; do not match with position mod 13
  85.            
  86.             int win=1;
  87.             for(int i=0;i<52;i++)
  88.                 if(deck[i]%13==i%13) win=0;
  89.            
  90.             if(win==1) countwin++;
  91.         }
  92.        
  93.         std::cout << "\nGames played: " << LIMIT << "\nGames won: " << countwin << "\nProportion: " << (double)countwin/LIMIT;
  94.         totalwins+=countwin;
  95.     }
  96.    
  97.     std::cout << "\nTotal games played: " << LIMIT*REPEATS << "\nTotal games won: " << totalwins << "\nProportion: " << (double)totalwins/(LIMIT*REPEATS); 
  98.    
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement