MATLAB: Converting a coin flip bet int oa bet on a house race game

while loop

You recently won $50 in a raffle. Instead of putting it in the bank like you know you should, you decide to go down to the racetrack and see how many races your money can last. The race track has horse races all summer long with each race consisting of 5 horses. You must wager $10 on each race. The horses with the highest chance of winning will always have lower payouts, while the horses with the lowest chance of winning will always have the highest payouts.
Assume the following conditions are true for all races:
  • The horse in gate 1 will always have a 16% of winning and a payout of $35.
  • The horse in gate 2 will always have a 20% of winning and a payout of $30.
  • The horse in gate 3 will always have a 28% of winning and a payout of $20.
  • The horse in gate 4 will always have a 24% of winning and a payout of $25.
  • The horse in gate 5 will always have a 12% of winning and a payout of $40.Write a MATLAB program that will ask for the user to pick a horse (1, 2, 3, 4, or 5) and then will use the “rand” or “randi” command to determine the winner of the race. After each race, the program will:
1) Adjust the total money accordingly (deduct bet, add payout)
2) Display the balance to the user
3) Ask the user to pick a new horse
4) Continue with the next race with a new random number determining the winning horse
This will continue until a balance of under $10 is reached. When a balance under $10 is reached, have MATLAB output to the user:
1) How many races their money lasted and
2) The maximum possible they could have won if they guessed correctly every time
cash=20;
maxcash=0;
attempts=0;
disp('So you''ve got $20 and you''re a betting man/woman?')
disp(' ')
disp('Tell you what - I''ll flip a coin and you guess heads or tails.')
disp(' ')
disp('If you guess right, I''ll give you $5.')
disp('If you guess wrong, I''ll take your $5.')
disp(' ')
disp('Let''s begin!')
disp(' ')
disp(' ')
%To flip a coin, say 1=heads and 2=tails, ask the user to guess:
guess=input('Please guess heads (1) or tails (2) for random coin flip: ');
%I want the code to iterate and let the user keep playing until they run
%out of money or quit:
while cash>=5 %We'll say it's $5 to bet each time
clc
cash=cash-5; %Take out $5 for starting bet
chance=randi(2); %Chance is either 1 or 2, 50% even chance for either
%chance=rand %Chance is a random number between 0 and 1
attempts=attempts+1;
if chance==1 %Hitting heads

flip=1;
elseif chance==2
flip=2;
end
if flip==guess %User guesses correctly
if flip==1 %Hitting heads
cash=cash+10; %They double their bet if they win
elseif flip==2 %Hitting tails
cash=cash+10; %Same as above
end
disp('Congrats, you guessed it!')
disp(['Your new cash total is $' num2str(cash) '!'])
elseif guess<1 || guess>2
disp('You didn''t follow instructions...')
disp('Thanks for the $5 donation!')
disp(['Your new cash total is $' num2str(cash) '!'])
else
disp('Sorry, you guessed incorrectly.. =(')
disp(['Your new cash total is $' num2str(cash) '!'])
end
if flip==1 %This if loop to capture max possible amount you could win
maxcash=maxcash+5;
elseif flip==2
maxcash=maxcash+5;
end
if cash>=5 %This IF loop will cancel prompting the user if they run out of money
disp('Please guess again for the next coin flip.')
guess=input('Put 1 for heads and 2 for tails: ');
end
end
%I want to display the result to the user:
disp(' ')
disp(['You made it for ' num2str(attempts) ' coin flips.'])
disp(' ')
disp(['The maximum you could have possibly won is:'])
disp(['$',num2str(maxcash)])

Best Answer

Here is a hint. This will let you see how you can use cumsum() and rand() to get the correct probability for each lane to win.
percentages = [0.16, 0.2, 0.28, 0.24, 0.12];
theSum = cumsum(percentages)
% Run 3000 races
numRaces = 3000;
horseHistogram = zeros(1, length(percentages)); % 5 bin histogram.
for race = 1 : numRaces
randomNumber = rand(1);
if randomNumber <= theSum(1)
% Horse #1 won
winningHorse = 1;
elseif randomNumber <= theSum(2)
% Horse #2 won
winningHorse = 2;
elseif randomNumber <= theSum(3)
% Horse #3 won
winningHorse = 3;
elseif randomNumber <= theSum(4)
% Horse #4 won
winningHorse = 4;
else
% Horse #5 won
winningHorse = 5;
end
horseHistogram(winningHorse) = horseHistogram(winningHorse) + 1;
end
for h = 1 : length(horseHistogram)
fprintf('Horse %d won %0.3f %% of the time.\n', h, 100 * horseHistogram(h) / numRaces);
end
theSum =
0.16 0.36 0.64 0.88 1
Horse 1 won 15.400 % of the time.
Horse 2 won 21.333 % of the time.
Horse 3 won 28.133 % of the time.
Horse 4 won 23.267 % of the time.
Horse 5 won 11.867 % of the time.
Just the percentages you'd expect.
Adapt as needed. You'll need to convert the for loop to a while loop
while cashRemaining >= 10
Then adjust cashRemaining based on loss or winnings for that race.