Consider the following code in r to generate value of a binomial random variable.
Binomial<-function(n,p,ns)
{
S <- rep(ns)
for (i in 1:ns)
{
k <-0
produ<-runif(1)
while (produ >=(1-p)^n)
{
produ <- produ*runif(1)
k <-k+1
}
S[i] <- k
}
return(S)
}
Is it correct?
If not, why not?
If yes, can be improved somehow?