I want to solve the following problem by simulation.
There is a bus with 100 labelled seats (labelled from 1 to 100). There are 100 persons standing in a queue. Persons are also labelled from 1 to 100.
People board on the bus in sequence from 1 to n. The rule is, if the person ‘i’ boards the bus, he checks if seat ‘i’ is empty. If it is empty, he sits there, else he randomly picks an empty seat and sits there. Given that 1st person picks seat randomly, find the probability that 100th person sits on his place i.e. 100th seat.
I found MATLAB code that performs the simulation,and I really want to learn how to implement the same algorithm in Mathematica. Will someone help me with this?
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;