SHARE
TWEET

CSE Blog 100 sided die probability problem

a guest Apr 8th, 2013 184 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. int testcount=150000; // number of simulated rounds
  8. bool playing[100]; for(int i=0; i<100; i++){playing[i]=1;} // whether a player is still active in a round
  9. int playingcount=100; // how many players are still active in a round
  10. int rollcount=0; // how many rolls there have been in a round
  11. int roundscore[100]; for(int i=0; i<100; i++){roundscore[i]=0;} // the round score for a player
  12. int roundhigh=0;
  13. long totalhigh=0;
  14. long totalscore[100]; for(int i=0; i<100; i++){totalscore[i]=0;} // the running total score for a player
  15. int wincount[100]; for(int i=0; i<100; i++){wincount[i]=0;} // the running total of rounds won for a player
  16. int ledcount[100]; for(int i=0; i<100; i++){ledcount[i]=0;} // the running total of rounds in the lead for a player
  17.  
  18. for(int i=0; i<testcount; i++){
  19.     while(playingcount>0){
  20.         int roll=rand()%100+1;
  21.         for(int b=0; b<100; b++){
  22.             if(playing[b] && roll>b){roundscore[b]=roll-rollcount; totalscore[b]+=roundscore[b]; playing[b]=0; playingcount--; if(roundscore[b]>roundhigh){roundhigh=roundscore[b];} if(totalscore[b]>totalhigh){totalhigh=totalscore[b];}}
  23.             }
  24.         rollcount++;
  25.         }
  26.     for(int j=0; j<100; j++){if(roundscore[j]==roundhigh){wincount[j]++;}}
  27.     for(int j=0; j<100; j++){if(totalscore[j]==totalhigh){ledcount[j]++;}}
  28.     for(int j=0; j<100; j++){playing[j]=1;}
  29.     playingcount=100;
  30.     rollcount=0;
  31.     for(int j=0; j<100; j++){roundscore[j]=0;}
  32.     roundhigh=0;
  33.     }
  34.  
  35. cout << "\nAfter " << testcount << " simulated rounds:";
  36. for(int b=0; b<100; b++){cout << "\nPlayer " << b+1 << " has $" << totalscore[b] << ", won " << wincount[b] << " rounds, and led for " << ledcount[b] << " rounds.";}
  37.  
  38. cin.get();
  39.  
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top