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)
numRaces = 3000;
horseHistogram = zeros(1, length(percentages));
for race = 1 : numRaces
randomNumber = rand(1);
if randomNumber <= theSum(1)
winningHorse = 1;
elseif randomNumber <= theSum(2)
winningHorse = 2;
elseif randomNumber <= theSum(3)
winningHorse = 3;
elseif randomNumber <= theSum(4)
winningHorse = 4;
else
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.
Best Answer