MATLAB: Roulette Algorithm Probability Loop

algorithmgamblelogloop

I am making a program that will run a roulette style program 1000000 times. The program should give me an output with my average winnings per trip to the casino. I am starting with 315 dollars and the conditions for betting are: if I lose i will double my bet and play again. If I win, I will start my bid back at my first initial bid amount (5 dollars) I have created a random number generator and an equation to determine my probability, I have having trouble with the loop. while numbers are generating I am trying to have the program do the bidding for me and my results are less than successful. Any ideas? I realize now that one of my equations in the loop is incorrect, I am just concerned with getting the loop to work at the moment. Thanks!
A = 315 %starting amount of money
initial = 5 %Initial bet amount
WIN = 335 %Condition for a win
LOSE = 0 %Condition for loss
profit = 0
g = 0 %counter for random numbers when multiplied by %probability
N = ((log(A+5)-log(5))/log(2)) %Number of bets
Pwinner = (1 – (1/2^N))^4 %Probability of a win
a = initial * (2^N -1) %bid condition for a loss
Gwinner = (1 – (10/19)^N)^4 %probability of win with green slots
%Allow Green Slots?
x = input('Do you want to allow for the green slots (0=No, 1=Yes)? ');
if x == 0;
r = randi([1,36],[1000000 ,1]);
round(r)
else
r = randi([1,38],[1000000 ,1]);
round(r)
end
fprintf('Please wait while computer simulates game 1000000 times');
%Solution while A < WIN && A > LOSE
g = Pwinner .* r;
round(g);
if g > 18;
A = A - initial
profit = (1/2^N) * A
else
fprintf('this isnt working')
end
end

Best Answer

I got it! Here is my code!!!
%Starting Over
%Algorithm #3
%Simulating Roulette
fprintf('\t\t\t\tSimulating Roulette\n\n')
%Constants and Variables
A = 315; %starting amount of money
initial = 5; %Initial bet amount
WIN = 335; %Condition for a win
LOSE = 0; %Condition for loss
betamount = initial; %set bet amount to 5
g = 1; %counter for random numbers
profit = 0; %profit counter
y = 1000000; %amount of games we wil play
N = ((log(A+5)-log(5))/log(2)); %Number of bets
Pwinner = (1 - (1/2^N))^4; %Probability of a win
a = initial * (2^N -1); %bid condition for a loss
Gwinner = (1 - (10/19)^N)^4; %probability of win with green slots
%Allow Green Slots?
x = input('Do you want to allow for the green slots (0=No, 1=Yes)? ');
%Generate random numbers that the game will use
if x == 0;
r = randi([1,36],[20000000 ,1]);
round(r);
else
r = randi([1,38],[20000000 ,1]);
round(r);
end
fprintf('Please wait while computer simulates game 1000000 times');
%Solution
for i = 1:y %Number of times the loop will run
A = 315; %Reset amount of money to 315
%every game
while A < WIN && A > LOSE %Condition for loop
if betamount > A %Can't over bid
betamount = A;
end
A = A - betamount; %Subtract bet from total money
if r(g)<= 18 %Condition for win/loss
A = A + (betamount *2); %Recalculates total money
betamount = initial; %Resets bet to 5
else
betamount = betamount * 2; %If I lose double my bet
end
g = g + 1; %used with r(g) to isolate integers
end
profit = profit + A; %counts my total profits
end
%Answer on New Screen
if x == 0 %Loop to print answer an a clear screen
clc
fprintf('\t\t\t\tSimulating Roulette\n\n');
fprintf('With only 18 red and 18 black (no green), you won 93.90 percent\n of the time you went to the casino\n\n')
fprintf('Your average winnings were %.2f per trip to the casino', (profit/y-315)) % profit/y-315 = profit per trip to the casino

else
clc
fprintf('\t\t\t\tSimulating Roulette\n\n');
fprintf('With 18 red, 18 black, and 2 green slots, you won 91.74 percent\n of the time you went to the casino\n\n')
fprintf('Your average winnings were %.2f per trip to the casino', (profit/y-315)) % profit/y-315 = profit per trip to the casino
end