Advertisement
Guest User

pig

a guest
Mar 4th, 2015
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.53 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <ctime>
  3. #include <iostream>
  4. #include <fstream>
  5.  
  6. const char FILENAME[] = "output.txt";
  7. const int DIE_SIZE = 100;
  8.  
  9. int g_holdAt = 0,
  10.     g_simulations = 0;
  11. double g_pigProb = 0,
  12.     g_probs[DIE_SIZE] = {};
  13.  
  14. void PrintTable(std::ostream& ostrm) {
  15.     //Formatting stuff.
  16.     ostrm.setf(std::ios::fixed);
  17.     ostrm.setf(std::ios::showpoint);
  18.     ostrm.precision(6);
  19.  
  20.     //Print table Header.
  21.     ostrm << "\nScore\tEstimated Probability" << std::endl;
  22.  
  23.     //Print probability for 0.
  24.     ostrm << 0 << "\t" << g_pigProb << std::endl;
  25.  
  26.     //Print other probabilities.
  27.     for (int i = 0; i < DIE_SIZE; ++i) {
  28.         ostrm << g_holdAt + i << "\t" << g_probs[i] << std::endl;
  29.     }
  30. }
  31.  
  32. int main() {
  33.     //Seed.
  34.     srand(333);
  35.  
  36.     //Get hold value and simulations from user.
  37.     std::cout << "What value should we hold at? ";
  38.     std::cin >> g_holdAt;
  39.     std::cout << "Hold-at-N turn simulations? ";
  40.     std::cin >> g_simulations;
  41.  
  42.     //Accumulate sums.
  43.     for (int i = 0; i < g_simulations; ++i){
  44.         int sum = 0;
  45.         while (sum < g_holdAt) {
  46.             int rollVal = rand() % DIE_SIZE + 1;
  47.             if (rollVal == 1) {
  48.                 sum = 0;
  49.                 break;
  50.             }
  51.             sum += rollVal;
  52.         }
  53.         if (sum == 0) {
  54.             g_pigProb++;
  55.         }
  56.         else {
  57.             g_probs[sum - g_holdAt]++;
  58.         }
  59.     }
  60.  
  61.     //Calc probabilities.
  62.     g_pigProb /= g_simulations;
  63.     for (int i = 0; i < DIE_SIZE; ++i) {
  64.         g_probs[i] /= g_simulations;
  65.     }
  66.  
  67.     //Print table to console.
  68.     PrintTable(std::cout);
  69.  
  70.     //Print table to file.
  71.     std::ofstream fout;
  72.     fout.open(FILENAME);
  73.     PrintTable(fout);
  74.     fout.close();
  75.  
  76.     return 0;
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement