We have seen the aeroplane boarding problem in the previous post. Let’s try and verify that using an R program. Here is the code:
x_rand <- c(1,2,3,4,5,6,7)
f_seat <- sample(x_rand, size = 1)
x_rand <- x_rand[-f_seat]
print(paste0(" Firat one takes ", f_seat))
print(paste0(x_rand, " remains"))
for (i in 2:6){
print(paste0("Next one is ", i))
if(i %in% x_rand){
x_rand <- x_rand[-which(x_rand == i)]
print(paste0(x_rand, " remains"))
} else {
n_seat <- sample(x_rand, size = 1)
x_rand <- x_rand[-which(x_rand == n_seat)]
print(paste0(x_rand, " remains"))
}
}
print(paste0(x_rand, " Seat for Last"))
if(x_rand == 7){
counter = 1
}else
counter = 0
And the output is:
[1] " Firat one takes 5"
[1] "1 remains" "2 remains" "3 remains" "4 remains" "6 remains" "7 remains"
[1] "Next one is 2"
[1] "1 remains" "3 remains" "4 remains" "6 remains" "7 remains"
[1] "Next one is 3"
[1] "1 remains" "4 remains" "6 remains" "7 remains"
[1] "Next one is 4"
[1] "1 remains" "6 remains" "7 remains"
[1] "Next one is 5"
[1] "1 remains" "6 remains"
[1] "Next one is 6"
[1] "1 remains"
[1] "1 Seat for Last"
Now, let us run the code for 100 passengers 10000 times and estimate the proportion for the last passenger to get the last seat.
success <- replicate(10000, {
x_rand <- seq(1:100)
f_seat <- sample(x_rand, size = 1)
x_rand <- x_rand[-f_seat]
for (i in 2:99){
if(i %in% x_rand){
x_rand <- x_rand[-which(x_rand == i)]
} else {
n_seat <- sample(x_rand, size = 1)
x_rand <- x_rand[-which(x_rand == n_seat)]
}
}
if(x_rand == 100){
counter = 1
}else
counter = 0
})
mean(success)
The answer is 0.5!
The proportion for the last passenger getting seat 1 is also 0.5. Try putting any other; the answer will be zero.