This shows you the differences between two versions of the page.
— |
cs-142:runs-in-random-numbers [2016/10/07 12:35] (current) kseppi created |
||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <code cpp> | ||
+ | // | ||
+ | // main.cpp | ||
+ | // Runsofthrees | ||
+ | // | ||
+ | // Created by Kevin Seppi on 10/7/16. | ||
+ | // Copyright © 2016 Kevin Seppi. All rights reserved. | ||
+ | // | ||
+ | #include <iostream> | ||
+ | #include <iomanip> | ||
+ | #include <stdlib.h> | ||
+ | #include <time.h> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int main(int argc, const char * argv[]) { | ||
+ | const int MAX_FACE = 6; | ||
+ | const int LOOKING_FOR = 3; | ||
+ | const int NUMBER_OF_HUMAN_ROLLS = 10; | ||
+ | const int NUMBER_OF_ROLLS = 100; | ||
+ | const int NUMBER_OF_SIMULATIONS = 1000; | ||
+ | | ||
+ | int dieFace = 0; | ||
+ | int currentRunLength = 0; | ||
+ | int longestRunLength = 0; | ||
+ | int totalLongestRunLength = 0; | ||
+ | double averageLongestRunLength = 0; | ||
+ | | ||
+ | cout << fixed << setprecision(2); | ||
+ | srand(static_cast <unsigned int>(time(0))); | ||
+ | // try the following line instead and run the program twice! | ||
+ | // srand(42); // Same sequence of random numbers every time (nothing special about 42, just a particular sequence) | ||
+ | | ||
+ | | ||
+ | cout << "Count runs of: " << LOOKING_FOR << " in rolls of a die:" << endl; | ||
+ | cout << "First you try being the random number generator, give me (all at once or one at a time) "; | ||
+ | cout << NUMBER_OF_HUMAN_ROLLS << " int's in the range 1-6: "; | ||
+ | | ||
+ | for (unsigned int i = 0; i < NUMBER_OF_HUMAN_ROLLS; i++) { | ||
+ | // Random roll of a 6 sided die | ||
+ | cin >> dieFace; | ||
+ | | ||
+ | if (dieFace == LOOKING_FOR) { | ||
+ | currentRunLength++; | ||
+ | longestRunLength = max(longestRunLength, currentRunLength); | ||
+ | } | ||
+ | else { | ||
+ | currentRunLength = 0; | ||
+ | } | ||
+ | } | ||
+ | cout << endl; | ||
+ | cout << "Summary:" << endl; | ||
+ | | ||
+ | cout << "The longest run of " << LOOKING_FOR << " was " << longestRunLength << endl; | ||
+ | cout << endl; | ||
+ | | ||
+ | cout << "...interesting, now let's let the computer make random values:" << endl; | ||
+ | cout << endl; | ||
+ | cout << "Count runs of: " << LOOKING_FOR << " in rolls of a die:" << endl; | ||
+ | | ||
+ | currentRunLength = 0; | ||
+ | longestRunLength = 0; | ||
+ | | ||
+ | cout << "Random values form a 6-sided die:" << endl; | ||
+ | for (unsigned int i = 0; i < NUMBER_OF_ROLLS; i++) { | ||
+ | // Random roll of a 6 sided die | ||
+ | dieFace = rand() % MAX_FACE + 1; | ||
+ | cout << dieFace << " "; | ||
+ | | ||
+ | if (dieFace == LOOKING_FOR) { | ||
+ | currentRunLength++; | ||
+ | longestRunLength = max(longestRunLength, currentRunLength); | ||
+ | } | ||
+ | else { | ||
+ | currentRunLength = 0; | ||
+ | } | ||
+ | } | ||
+ | cout << endl; | ||
+ | cout << "Summary:" << endl; | ||
+ | | ||
+ | cout << "The longest run of " << LOOKING_FOR << " was " << longestRunLength << endl; | ||
+ | cout << endl; | ||
+ | | ||
+ | cout << "...interesting, but what is the AVERAGE of the LONGEST runs over 1000 simulations?" << endl; | ||
+ | cout << "Let's try it:" << endl; | ||
+ | cout << endl; | ||
+ | cout << "Simulating..." << endl; | ||
+ | totalLongestRunLength = 0; | ||
+ | for (unsigned int i = 0; i < NUMBER_OF_SIMULATIONS; i++) { | ||
+ | currentRunLength = 0; | ||
+ | longestRunLength = 0; | ||
+ | for (unsigned int j = 0; j < NUMBER_OF_ROLLS; j++) { | ||
+ | dieFace = rand() % MAX_FACE + 1; | ||
+ | | ||
+ | if (dieFace == LOOKING_FOR) { | ||
+ | currentRunLength++; | ||
+ | longestRunLength = max(longestRunLength, currentRunLength); | ||
+ | } | ||
+ | else { | ||
+ | currentRunLength = 0; | ||
+ | } | ||
+ | } | ||
+ | //cout << longestRunLength << endl; | ||
+ | totalLongestRunLength += longestRunLength; | ||
+ | } | ||
+ | cout << endl; | ||
+ | cout << "Summary:" << endl; | ||
+ | | ||
+ | averageLongestRunLength = totalLongestRunLength/(static_cast<double>(NUMBER_OF_SIMULATIONS)); | ||
+ | | ||
+ | cout << "The average longest run of " << LOOKING_FOR << " was " << averageLongestRunLength << endl; | ||
+ | cout << endl; | ||
+ | | ||
+ | system("pause"); | ||
+ | return 0; | ||
+ | } | ||
+ | |||
+ | </code> |