# This code defines a procedure which acts just like a function # and it plays one game of Roulette and reports your winnings # or losses based on a bet of $a on a main number, and a bet $b # on two different splits between that main number and two adjacent # numbers game := proc(a,b) u := rand()/10^12; win=0; if (u < 1/38) then win :=35*a + 34*b elif (u <= 3/38) then win := 16*b - a else win := -(a + 2*b) end if; win end proc; # To play a game just type: game(1, 2); # for instance # The following plays a series of games until # you have reached a limit (Stop) or cannot place # another bet # The number of games played and the amount of money in # your pocket when you stop playing are given as output # B represents how much money you have in your pocket play := proc(Stop, Money) # Stop and Money are formal parameters # They cannot be modified in the code below # Since we want the amount of money we have # in our pockets to vary and want to keep # track of the maximum we have ever had we let # B = Money and the current value of Bmax to be # Money too. B := Money; Bmax := Money; a := 1; b := 2; bet := a + 2*b; number_games := 0; while(B a + 2*b) do win := game(a,b); B := B + win; # if you have more money in your pocket than # ever before reset your bets to their lowest # level if(B >= Bmax) then Bmax := B; a := 1; b := 2; # else until you can recoup your losses, # reset the bets to be higher elif (35*a + 34*b + B < Bmax) then if (a < b) then a := a+1; else b:=b+1; end if; end if; number_games := number_games + 1 end do: [number_games, B] end proc; # Now let us play the game many times and see # if, on average, we make of lose money Stop := 2000: Money := 1500: number_simulations := 1000: wins := 0: outcomes := Array(1..2, 1..number_simulations): for i from 1 to number_simulations by 1 do out := play(Stop, Money); outcomes[1,i] := out[1]; outcomes[2,i] := out[2]; if (out[2] >=2000) then wins := wins + 1 end if; end do: evalf(wins/number_simulations); # Print out the average and standard deviation # for the number of games played. with(Statistics): Mean(outcomes[1, 1..number_simulations]); StandardDeviation(outcomes[1, 1..number_simulations]); # Print out the average and standard deviation # for the amount of money in your pocket at the end. Mean(outcomes[2, 1..number_simulations]); StandardDeviation(outcomes[2, 1..number_simulations]);