Search code examples
rfunctionpromptbirthday-paradox

Birthday Paradox - Function with input variable


I am trying to simulate the probability that more than two students have the same birthday in a room full on n people. Currently I think my code is working properly, although I have to initially just run the first line of code to select my n value, then run the rest of the code separately (see below)

n = as.integer(readline(prompt = "Enter the number of students in a room:"))

sims = 10000
x = numeric(sims)

for (i in 1:sims){
s = sample(1:365, n, replace=TRUE)
x[i] = n - length(unique(s))}

samebday = length(which(x>0))/length(x)
samebday

How would I tidy this up so that the variable n is contained within the function? As soon as I try to convert this to a function as follows:

bday.prob = function(n){...}

then errors start occurring.


Solution

  • If you want to use the code you wrote before and simply wrap it into a function, you can do so by letting n and sims be user-defined input variables, like @42- mentioned.

    Below is my solution, with minimal changes from what you provided:

    bday.prob = function(n, sims){
      #' @param n is the number of students in a room; user-defined
      #' @param sims is the number of trials; user-defined
    
      x = numeric(sims)
      for (i in 1:sims){
        s = sample(1:365, n, replace=TRUE)
        x[i] = n - length(unique(s))
      }
      samebday = length(which(x > 0))/length(x)
      return(samebday)
    }
    

    Use the function as follows:

    bday.prob(n=<User choice>, sims=<User choice>)
    

    or

    bday.prob(n=as.numeric(readline(prompt = "Enter the number of students in a room:")), sims=100)
    ## Enter the number of students in a room: <User choice>