You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Inverse CDF Method to Generate Random Variable cc @mathsorosThe following R code is used to generate certain number of random variables under N(0,1) distribution. The main function is ?sampler? and it take an integer as input.
## Main function ##
sampler <- function(n){
rv <- NULL ## Initialization ##
for (i in 1:n){
Y <- runif(1,min=0,max=1)
## Change this to generate rv's under other distribution ##
X <- qnorm(Y,mean=0,sd=1)
rv <- c(rv,X)
}
return(rv)
}
## Generate 10000 N(0,1) random variables ##
genrv <- sampler(10000)
## Plot histogram of samples ##
hist(genrv,30,prob=TRUE,xlab='X',
main='Normal random samples generated by inverse CDF')