Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <ctime>
- #include <iostream>
- #include <fstream>
- const char FILENAME[] = "output.txt";
- const int DIE_SIZE = 100;
- int g_holdAt = 0,
- g_simulations = 0;
- double g_pigProb = 0,
- g_probs[DIE_SIZE] = {};
- void PrintTable(std::ostream& ostrm) {
- //Formatting stuff.
- ostrm.setf(std::ios::fixed);
- ostrm.setf(std::ios::showpoint);
- ostrm.precision(6);
- //Print table Header.
- ostrm << "\nScore\tEstimated Probability" << std::endl;
- //Print probability for 0.
- ostrm << 0 << "\t" << g_pigProb << std::endl;
- //Print other probabilities.
- for (int i = 0; i < DIE_SIZE; ++i) {
- ostrm << g_holdAt + i << "\t" << g_probs[i] << std::endl;
- }
- }
- int main() {
- //Seed.
- srand(333);
- //Get hold value and simulations from user.
- std::cout << "What value should we hold at? ";
- std::cin >> g_holdAt;
- std::cout << "Hold-at-N turn simulations? ";
- std::cin >> g_simulations;
- //Accumulate sums.
- for (int i = 0; i < g_simulations; ++i){
- int sum = 0;
- while (sum < g_holdAt) {
- int rollVal = rand() % DIE_SIZE + 1;
- if (rollVal == 1) {
- sum = 0;
- break;
- }
- sum += rollVal;
- }
- if (sum == 0) {
- g_pigProb++;
- }
- else {
- g_probs[sum - g_holdAt]++;
- }
- }
- //Calc probabilities.
- g_pigProb /= g_simulations;
- for (int i = 0; i < DIE_SIZE; ++i) {
- g_probs[i] /= g_simulations;
- }
- //Print table to console.
- PrintTable(std::cout);
- //Print table to file.
- std::ofstream fout;
- fout.open(FILENAME);
- PrintTable(fout);
- fout.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement