Question about "runif"

Hello everyone I'm kinda new to RStudio and there's something I don't understand.

How can this code work:

simu<-function(t){
u<-runif(1,0,1)
return(1-sqrt(u))
}

There is no "t" anywhere in my function but it still works, so because it confused me I tried to run this instead:

simu2<-function(t){
u<-runif(1,0,1)
return(u)
}

And again it worked, so I guess I don't understand clearly what runif does, is it actually a function ? I thought it could only generates random number that follow a uniform law.

Thx in advance

Yes, runif() is a function that generates a random value within a given range. Your function is equivalent to

simu<-function(t){
  u<-runif(n = 1,min = 0,max = 1)
  return(1-sqrt(u))
}

You have set runif to give one sample in the range [0,1]. You can call simu() with or without a value of t because your function does not use t.
If you were to call simu() without a value of t and if simu() used t in some way, that would generate an error.

Your function gets the argument t. It just doesn't use t for anything, so it ignores it.

Ok i understood thx a lot, but I found this code on one of my course, so what's the point to create a function for something which is not ?

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.