So I'm working on a stats problem and the question is "Six kids are standing in line. What is the probability that they are in alphabetical order by name? Assume no two children have the same exact name." And I'm using the sample() and rle() functions in R but I'm confused on how to calculate the probability. May I get some help?
Also this is the code I have so far:
kids <- sample(c("A", "B", "C", "D", "E", "F"), 6, replace = TRUE)
table(kids)
head(kids)
rle(c("A", "B", "C", "D", "E", "F"))
kids.rle <- rle(kids)
str(kids.rle)
sort(kids.rle$lengths, decreasing = TRUE)
As @YOLO stated, the theoretical probability is 1 / 720
, which can be computed as 1 / factorial(6)
in R. However, you can easily calculate this in your head as well. Computational power comes handy if you want to run a small simulation to show that the observed probability converges to the theoretical one as the number of repetition increases:
kids_ordered <- c("A", "B", "C", "D", "E", "F")
n <- 1000000 # number of repetition
result <- rep(NA, n) # vector to hold outcomes
set.seed(147) # seed for reproducibility
# reorder kids n times and check if the outcome is in alphabetical order each time
for(i in seq_len(n)) {
result[i] <- all(sample(kids) == kids_ordered)
}
# compute the probability
mean(result)
# [1] 0.001376
The result is very close to 1 / 720 = 0.001389