dnorm(x, mean=0, sd=1) pnorm(q, mean=0, sd=1) qnorm(p, mean=0, sd=1) rnorm(n, mean=0, sd=1)
The ratio of uniform deviates is used by rnorm to generate normal deviates (Kinderman and Monahan, 1977). The average number of uniform deviates used per normal deviate is 8/sqrt(exp(1)*pi) which is about 2.74.
Kinderman, A. J. and Monahan, J. F. (1977). Computer generation of random variables using the ratio of uniform deviates. ACM Transactions on Mathematical Software. 3 257-260.
rnorm(20, 0, 10) #sample of 20, mean 0, standard dev. 10# a function to generate random multivariate Gaussians rmultnorm <- function(n, mu, vmat, tol = 1e-07) { p <- ncol(vmat) if(length(mu)!=p) stop("mu vector is the wrong length") if(max(abs(vmat - t(vmat))) > tol) stop("vmat not symmetric") vs <- svd(vmat) vsqrt <- t(vs$v %*% (t(vs$u) * sqrt(vs$d))) ans <- matrix(rnorm(n * p), nrow = n) %*% vsqrt ans <- sweep(ans, 2, mu, "+") dimnames(ans) <- list(NULL, dimnames(vmat)[[2]]) ans }