The question is this: each person checks their hat at the door but then for some reason, the hat check attendant messes up the record and cannot remember which hat belongs to whom. The attendant decides to return the hats to everyone randomly.
I want to conduct a stimulation in R that estimates:
- The probability that no one gets the hat back correctly.
- The probability that at least one patron gets his or her own hat back.
- Average number of patrons who get their own hats back.
For the simulation, let's set n=5
.
I am thinking about assigning
hats <- c(1:5)
patrons <- c(1:5)
and make a function (a,b){a-b}
I got a little bit confused about how the R works, because I used to use Python and they have a different structure. But my thought process is like this:
patrons = float(input("How many people attend? "))
def number_of_patrons_assign:
for i in patrons:
return i
def number_of_hats_assign:
for r in hats:
return r
def counting:
list=[]
if number_of_patrons_assign == number_of_hats_assign
return list
Sorry this might be wordy, but I haven't use Python for a year.
My partner uses R language and wrote this :
hats <- c(1:5)
patrons <- c(1:5)
vector <- NULL
test <- function(a, b)
{
a-b
}
p <- 0
for(n in 1:10)
{
x <- sample(hats, 5, replace = FALSE)
y <- sample(patrons, 5, replace = FALSE)
test(x, y)
vector[n] <- c(if(test(x, y)==0) p <- 0,
ifelse(test(x, y)==9, p <- 0, P <- 1))
}
I don't get what the function of the NULL
there and how that works? This works but doesn't really look like what we are looking for.
vector <- NULL
is just a bad way to initialize a vector vector to be filled up in thefor
loop – Rich Scriven