Probability airport question code.

3 次查看(过去 30 天)
Matthew Lozancich
Matthew Lozancich 2017-11-20
回答: Torsten 2017-11-21
So I've been working on this problem for hours and now I'm stuck.. I can't figure it out. I know my probability is messed up, in fact I think it should be around 50%?
You are at the airport waiting in line to board a plane. There are 99 other passengers and unfortunately for you, you are last in line. At the front of the line you recognize a colleague, Frank. Frank is known to randomly sit where he pleases instead of his assigned seat. All the other passengers, including yourself, behave in a more sane fashion:
  • They will look to sit in their assigned seat, if that seat is available, theywill sit in their assigned seat.
  • On the other hand, if their assigned seat is taken, they will pick a free seatat random to sit in.
  • Create a function that runs a 10,000 iteration Monte Carlo simulation that estimates the probability that your seat will be taken
function boarding
%100 passangers, so assuming we have 100 seats
%Frank has a 1 percent chance of sitting in his assigned set if he picks at
%random
trials = 10000;
P=100;
inmyseat=0;
x=0;
for i=1:trials
x=randi([1,100],1);
if x==1 %if x==1 then it means Frank sat in his own seat
break
else
for P=(99:-1:2)
y=randi([1,P],1);
if y==1 %this means that if y==1 then they sat in my seat
inmyseat = inmyseat+1;
break
end
end
end
end
prob=(inmyseat/trials)*100
end

回答(1 个)

Torsten
Torsten 2017-11-21
function boarding
%100 passengers
%We assume that Frank's seat is seat 99, your seat is seat 100.
%Further assume that seat i belongs to passenger i+1 (i=1,...,98)
trials = 10000;
P = 100;
inmyseat = 0;
for i=1:trials
outcome = 0;
start = 1;
while outcome == 0
x = randi([start,P],1);
if x == P-1
outcome = 1;
inmyseat = inmyseat+1;
elseif x == P
outcome = 1;
end
start = x+1;
end
end
prob = inmyseat/trials*100;
Best wishes
Torsten.

类别

Help CenterFile Exchange 中查找有关 General Applications 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by