Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Based on refutation to given solution to Riddler Express at http://fivethirtyeight.com/features/dont-throw-out-that-calendar/#ss-1
- //sample output:
- /*
- Games played: 2000000
- Games won: 32271
- Proportion: 0.0161355
- Games played: 2000000
- Games won: 32377
- Proportion: 0.0161885
- Games played: 2000000
- Games won: 32272
- Proportion: 0.016136
- Games played: 2000000
- Games won: 32450
- Proportion: 0.016225
- Games played: 2000000
- Games won: 32500
- Proportion: 0.01625
- Games played: 2000000
- Games won: 32304
- Proportion: 0.016152
- Games played: 2000000
- Games won: 32554
- Proportion: 0.016277
- Games played: 2000000
- Games won: 32548
- Proportion: 0.016274
- Games played: 2000000
- Games won: 32682
- Proportion: 0.016341
- Games played: 2000000
- Games won: 32466
- Proportion: 0.016233
- Total games played: 20000000
- Total games won: 324424
- Proportion: 0.0162212
- */
- #include <random>
- #include <iostream>
- #include <sstream>
- #include <chrono>
- #define LIMIT 2000000
- #define REPEATS 10
- //std::random_device rd;
- unsigned seed = static_cast<int> (std::chrono::system_clock::now().time_since_epoch().count());
- std::mt19937 rval(seed);
- int main(int argc, char** argv)
- {
- int deck[52];
- int totalwins=0;
- for(int kk=0;kk<REPEATS;kk++)
- {
- int countwin=0;
- for(int k=0;k<LIMIT;k++)
- {
- for(int i=0;i<52;i++) deck[i]=-1;
- for(int i=51;i>=0;i--)
- {
- unsigned int j=rval()%(i+1);
- int pos=0;
- while(j>0 || deck[pos]>=0)
- {
- if(deck[pos]>=0) pos++;
- else {pos++; j--;}
- }
- deck[pos]=i;
- }
- //flip over card; do not match with position mod 13
- int win=1;
- for(int i=0;i<52;i++)
- if(deck[i]%13==i%13) win=0;
- if(win==1) countwin++;
- }
- std::cout << "\nGames played: " << LIMIT << "\nGames won: " << countwin << "\nProportion: " << (double)countwin/LIMIT;
- totalwins+=countwin;
- }
- std::cout << "\nTotal games played: " << LIMIT*REPEATS << "\nTotal games won: " << totalwins << "\nProportion: " << (double)totalwins/(LIMIT*REPEATS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement