aboutsummaryrefslogtreecommitdiffstats
path: root/ee1301/wk3/hw3_directory/strap012_HW3A.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ee1301/wk3/hw3_directory/strap012_HW3A.cpp')
-rw-r--r--ee1301/wk3/hw3_directory/strap012_HW3A.cpp45
1 files changed, 25 insertions, 20 deletions
diff --git a/ee1301/wk3/hw3_directory/strap012_HW3A.cpp b/ee1301/wk3/hw3_directory/strap012_HW3A.cpp
index 07dc6b5..38c2a2b 100644
--- a/ee1301/wk3/hw3_directory/strap012_HW3A.cpp
+++ b/ee1301/wk3/hw3_directory/strap012_HW3A.cpp
@@ -14,6 +14,30 @@ One-armed Bandit Simulator
#include <iomanip>
using namespace std;
+int spin_the_wheels(int d, int w);
+
+int main () {
+ srand(time(NULL));
+ int w, d, winCount, winTest;
+ for (w=3; w<=6; w++) {
+ winCount=0;
+ for (d=9; d<=27; d++) {
+ winCount=0;
+ for (long n=1000000; n>0; n--) { //The simulation is always done 1,000,000 times
+ int winTest = spin_the_wheels(d, w);
+ winCount+=winTest;
+ }
+ if (w==6) {
+ cout << scientific;
+ }
+ cout << "w=" << w << ", d=" << d
+ << ": Simulated probability = m/n = " << (winCount / 1000000.0) * 100.0 << "%. "
+ << "Theoretical probability = " << (d / (pow(d, w))) * 100 << "%." << endl;
+ d+=2;
+ }
+ }
+}
+
// Function: spin_the_wheels
// ---------------------------
// Simulates the one-armed bandit from the previous HW
@@ -24,7 +48,7 @@ using namespace std;
int spin_the_wheels(int d, int w) {
int spinOG=0, spinNew=0, win=0;
spinOG = ( (rand() % d) + 1); //Original spin is always the same
- w-=1;
+ w-=1; //The first wheel was spun as spinOG
for (int i=0; i<w; i++) {
spinNew = ( (rand() % d) + 1);
if (spinOG==spinNew) {
@@ -36,23 +60,4 @@ int spin_the_wheels(int d, int w) {
} else {
return 0;
}
-}
-
-int main () {
- srand(time(NULL));
- int w, d, winCount, winTest;
- for (w=3; w<=6; w++) {
- winCount=0;
- for (d=9; d<=27; d++) {
- winCount=0;
- for (long n=1000000; n>0; n--) {
- int winTest = spin_the_wheels(d, w);
- winCount+=winTest;
- }
- cout << "w=" << w << ", d=" << d
- << ": Simulated probability = m/n = " << (winCount / 1000000.0) * 100.0 << "%. "
- << "Theoretical probability = " << (d / (pow(d, w))) * 100 << "%." << endl;
- d+=2;
- }
- }
} \ No newline at end of file