diff options
Diffstat (limited to 'ee1301/wk3/hw3_directory')
-rw-r--r-- | ee1301/wk3/hw3_directory/strap012_HW3A.cpp | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/ee1301/wk3/hw3_directory/strap012_HW3A.cpp b/ee1301/wk3/hw3_directory/strap012_HW3A.cpp index fd051fd..e22ef89 100644 --- a/ee1301/wk3/hw3_directory/strap012_HW3A.cpp +++ b/ee1301/wk3/hw3_directory/strap012_HW3A.cpp @@ -11,33 +11,32 @@ One-armed Bandit Simulator #include <iostream> #include <stdlib.h> #include <cmath> -#include <iomanip> +#include <iomanip> using namespace std; + int spin_the_wheel(int d, int w) { int spinOG, spinNew; spinOG = rand() % d + 1; - for (int i=w; i>0; i--) { + for (int i=1; i<w; i++) { spinNew = rand() % d + 1; - if (spinNew==spinOG) { - //Do nothing - } else { + if (spinNew!=spinOG) { return 0; } } return 1; } int main () { - long n=0; - int m=0; - float d= 3; - float w= 9; - for (n; n<=1000000; n++) { - m = spin_the_wheel(d,w); - if (m==1) { - m++; - } + long n = 0; + int m = 0; + int test; + int w = 3; + int d = 9; + for (n; n <= 1000; n++) + { + test = spin_the_wheel(d, w); + m+=test; } cout << "w=" << w << ", d=" << d; - cout << ": Simulated probability = m/n = " << (m/n)*100 << ". "; - cout << "Theoretical probability = " << (d/pow(d,w))*100 << "." << endl; + cout << ": Simulated probability = m/n = " << (m/n)*100 << "%. "; + cout << "Theoretical probability = " << (d/(pow(d,w)))*100 << "%." << endl; }
\ No newline at end of file |