This is a working model to show the mathmatical solution to a probability problem and the
brute force simulation method as well.
The original problem asks: There are 4 people on an elevator, in a building with 5 floors. If each person has the same odds of getting off on any given floor, what is the likelihood that they all get off on different floors?
To solve that its basically sampling without replacement. Or 5/5 (first person to leave the elevator has 100% chance of getting off on a different floor. Then 4/5, the 2nd person has an 80% chance of getting off on a different floor. Then 3/5 and 2/5. So its 5/5*4/5*3/5*2/5 or 19.2%.
So I built this to expand on it and ask: if you have n people in a building with x floors, what is the chance they all get off on different floors?
To solve that mathmatically in R:
And to solve it with a simulation:
The original problem asks: There are 4 people on an elevator, in a building with 5 floors. If each person has the same odds of getting off on any given floor, what is the likelihood that they all get off on different floors?
To solve that its basically sampling without replacement. Or 5/5 (first person to leave the elevator has 100% chance of getting off on a different floor. Then 4/5, the 2nd person has an 80% chance of getting off on a different floor. Then 3/5 and 2/5. So its 5/5*4/5*3/5*2/5 or 19.2%.
So I built this to expand on it and ask: if you have n people in a building with x floors, what is the chance they all get off on different floors?
To solve that mathmatically in R:
Probability = matrix(NA,People)
# get the individual probability per person
for(a in 1:People){
Probability[a] = (Floors - (b - 1))/Floors
} # and to run it for each person, decreasing by person
prod(Probability)
And to solve it with a simulation:
Obs = matrix(NA,nSims) # get the true/false case over multiple observations
for(b in 1:nSims){
x = sample(Floors, People, replace = TRUE,
prob = rep((1/Floors),Floors))
if (length(unique(x)) == People) {Obs[b] = 1} else {Obs[b] = 0}
}
mean(Obs)
Mathmatical Method:
Simulation Method: