Advertisement
Guest User

Project SIA Program

a guest
Dec 17th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <cstdlib>
  4. #include <ctime>
  5. #include <random>
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.  
  11. srand(time(NULL)); // Seed - the specific number used to generate random numbers - change to get different results
  12. srand(rand());
  13. //Variable setup
  14.  
  15. double nDeck = 52; //Size of deck
  16. int kCards = 10; //Amount of cards picked
  17. int dataSize = 1000000; //Amount of times simulation is run
  18. int playerCards[kCards]; //Arrangement of player's cards
  19. int houseCards[kCards]; //Arrangement of house's cards
  20. int trial[dataSize]; //Stores the amount of matches for each trial
  21. double totalMatches[kCards+1]; //Stores the amount of matches for each specific case
  22. int pick = 0; // card picked
  23. int matches = 0; // amount of matches in a speccific situation
  24.  
  25. for (int i = 0; i < kCards; i++) //Required for checking repeats
  26. {
  27. playerCards[i] = 0;
  28. houseCards[i] = 0;
  29. totalMatches[i] = 0;
  30. }
  31. totalMatches[10] = 0;
  32.  
  33.  
  34.  
  35. default_random_engine generator; // uniform random number generation
  36. uniform_real_distribution<double> distribution(0.0, nDeck);
  37. //uniform_int_distribution<int> distribution(0, nDeck);
  38.  
  39.  
  40.  
  41. for (int r = 0; r < dataSize; r++) //Start of simulation
  42. {
  43.  
  44. for (int i = 0; i < kCards; i++) //Picking cards for the house
  45. {
  46.  
  47. pick = (rand() % 52) + 1; //Random card picked
  48. //pick = (int) distribution(generator);
  49.  
  50. for(int o = 0; o < kCards; o++) //Checking that the card was not picked before
  51. {
  52.  
  53. if (pick == houseCards[o])
  54. {
  55. i--; // This value was invalid, find another value
  56. break; // Was a repeat, find another random value
  57. }
  58.  
  59. }
  60.  
  61. houseCards[i] = pick;
  62.  
  63. }
  64.  
  65.  
  66.  
  67. for (int i = 0; i < kCards; i++) //Picking cards for the player
  68. {
  69. pick = rand() % 52 + 1; //Random card picked
  70. // pick = (int) distribution(generator);
  71.  
  72. for(int o = 0; o < kCards; o++) //Checking that the card was not picked before
  73. {
  74.  
  75. if (pick == playerCards[o])
  76. {
  77. i--; // This value was invalid, find another value
  78. break; // Was a repeat, find another random value
  79. }
  80.  
  81. }
  82.  
  83. playerCards[i] = pick;
  84.  
  85. }
  86.  
  87. for(int k = 0; k < kCards; k++)// Compare all of the house's and player's cards to check for any matches
  88. {
  89.  
  90. for(int l = 0; l < kCards; l++)
  91. {
  92.  
  93. if (houseCards[k] == playerCards[l])
  94. {
  95. matches++;
  96. }
  97.  
  98. }
  99.  
  100. }
  101.  
  102. trial[r] = matches;
  103. matches = 0;
  104. }
  105.  
  106.  
  107. int temp = 0;
  108. for (int i = 0; i < dataSize; i++) // Adds each individual trial to count the total amount of matches
  109. {
  110.  
  111. temp = trial[i]; // temp must be used for g++ compiler
  112.  
  113. temp =totalMatches[temp]+1;
  114.  
  115. totalMatches[ trial[i] ] = temp;
  116.  
  117. }
  118.  
  119.  
  120.  
  121. for (int i = 0; i < kCards + 1; i++) //Output of the results
  122. {
  123. cout << "Matches of " << i << "'s: " << totalMatches[i] << "." << endl;
  124. }
  125.  
  126. cout << endl;
  127.  
  128. double temp2 = 0.0000001; // For the probability of the outcome
  129. double temp3 = 0;
  130.  
  131. for (int i = 0; i < kCards + 1; i++) // probability of outcome
  132. {
  133. temp2 = 100*(totalMatches[i]/dataSize);
  134. cout << "Probability for matches of " << i << "'s: " << temp2 << " %" << endl;
  135. temp3 += temp2;
  136. }
  137.  
  138. cout << "Total Percentage: " << temp3 << endl;
  139.  
  140. return 0;
  141. }
  142. //in the case of 52 cards and select 10
  143. //0: .09301 3 USD 09.301%
  144. //1: .28185 0 USD 28.185%
  145. //2: .33574 0 USD 33.574%
  146. //3: .20464 0 USD 20.464%
  147. //4: .06963 3 USD 06.963%
  148. //5: .01355 10 USD 01.355%
  149. //6: .00149 100 USD 00.149%
  150. //7: .00009 1,000 USD 00.009%
  151. //8: <.00001 10,000 USD 00.001%
  152. //9: <.00001 100,000 USD 00.001%
  153. //10: <.00001 1,000,000 USD 00.001%
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement