;Results: ;Since random numbers control the results of this function, ;the exact output is not predictable. ;Indeed, for small values of numBets the results are ;accentually random. ;However, if written correctly, large values of numBets ;will give consistent results for a given value of maxBet. ; ;For example, if maxBet is 1, then every bet is 1 dollar. ; Thus, after 1,000,000 bets, there will be very close to ; 1,000,000 * 18/38 wins and 1,000,000 * 20/38 losses. ; After 1,000,000 bets, it is highly predictable that ; you will loose between 50,000 and 55,000 dollars. ;The results are not so obvious for larger values of ; of maxBet, hence the reason writing a simulation. ;When maxBet is 1, then every bet is an independent event. ;However, if maxBet is 512, then a single event may be ;composed of as many as 10 bets. Thus, as maxBet gets ;larger, a given number of bets will consist of less ;events and, therefore, have less predictable results. ;If implemented correctly, then numBets = 1,000,000 bets ;will with high probability, be within the following bounds: ;maxBet = 1: loose between $50,000 and $55,000. ;maxBet = 2: loose between $50,000 and $100,000. ;maxBet = 5,000: loose between $250,000 and $1,500,000. ;maxBet = 50,000: loose between $200,000 and $2,000,000. ;maxBet = 500,000: loose between $200,000 and $2,000,000. ;maxBet = 5,000,000: win between $450,000 and $500,000 ; (with 1 in 29 chance of loosing 4 million). ;maxBet = 50,000,000: win between $450,000 and $500,000. ; (with 1 in 273 chance of loosing 34 million). ;Notice that in the last two cases, the result is winning ;almost half a million dollars. ;This is dependant on there NEVER being a loosing ;streak that cannot be covered by the betting strategy. ;A single breaking loosing streak at this magnitude, ;cannot be recovered in 1 million bets even if every other ;bet is a win on the first try. ;The rate at which the bet increases is exponential: (2^n). ;However, the rate of the probability of a catastrophic ;loosing streak decreases exponentially: (18/38)^n. ;n 2^n (18/38)^n ;1 1 4.74E-01 ;2 2 2.24E-01 ;3 4 1.06E-01 ;4 8 5.03E-02 ;5 16 2.38E-02 ;10 512 5.69E-04 ;11 1,024 2.69E-04 ;12 2,048 1.28E-04 ;13 4,096 6.04E-05 ;14 8,192 2.86E-05 ;15 16,384 1.36E-05 ;20 524,288 3.23E-07 ;23 4,194,304 3.44E-08 ;26 33,554,432 3.65E-09 ;29 268,435,456 3.88E-10 ;Thus, with 1 million bets and a $34 million bet limit, ;there is a 1,000,000*3.65E-09 (=1/273) chance of loosing ;34 million and a (272/273) chance of winning $470,000. ;As maxBet gets larger, the chance of loosing decreases ;a bit faster than the cost of loosing. ;For example, with a max bet of 268 million, the chance ;of loosing after 1 million bets is 1,000,000*3.88E-10 ;(=1/2500) ;============== Grading Rubric ================== ;The assignment is worth 10 points. ;Below are 7 test cases. ;Score two points if you handel the three error cases with some ; type of message and do not just crash, quit, or try to ; loop forever. ; ;Score 4 points if your results come within the ranges shown ; on the four non-error test cases. ; ;Score two points for having good comments. ; ;Score two points if John can quickly figure out ; how to download, save, run and test your code. (define roulette (lambda (numBets maxBet) (define myMoney 0) (define currentBet 1) (cond ((or (not (integer? numBets)) (< numBets 1)) '("Error: numBets must be a positive integer.") ) ((or (not (real? maxBet)) (< maxBet 1)) '("Error: maxBet must be at least 1.0.") ) (else ;Example of Scheme do: ;(do ((i 0 (+ i 1))) ((>= i 5)) ; (display i) ; (display " ") ;) ;(newline) ;Output: 0 1 2 3 4 (do ((i 0 (+ i 1))) ((>= i numBets)) ;Place a bet (set! myMoney (- myMoney currentBet)) (cond ;Note: for a 5x speed up, use (/ 18.0 38.0) ; rather than exact arithmetic: (/ 18 38). ((< (random) (/ 18.0 38.0)) ;I win!!! (set! myMoney (+ myMoney (* 2 currentBet))) (set! currentBet 1) ) (else ;I lose :( (set! currentBet (* 2 currentBet)) (if (> currentBet maxBet) (set! currentBet 1)) ) ) ) myMoney ) ) ) ) ;============== Unit Tests ====================== (load "test.scm") (define msg "roulette") (test msg (roulette -1 1) '("Error: numBets must be a positive integer.")) (test msg (roulette 1.5 1) '("Error: numBets must be a positive integer.")) (test msg (roulette 1 0.5) '("Error: maxBet must be at least 1.0.")) (roulette 1000000 1) ; Loose between $50,000 and $55,000. (roulette 1000000 2) ; Loose between $50,000 and $100,000. (roulette 1000000 5000) ; Loose between $250,000 and $1,500,000. (roulette 1000000 50000000) ; Win between $450,000 and $500,000