### Generate some special random variables from others. # generate gamma(k) from exponential k <- 10 nLength <- 10^4 f1 <- function() { # generate exp(1) random variables mExp <- matrix(-log(runif(nLength*k,0,1)),ncol=nLength) # gamma(k) is sum of k exponentials vRes <- apply(mExp, 2, sum) } # generate gamma(k) with R f2 <- function() { vRes <- rgamma(nLength,k) } # compare execution times -> the inbuilt method is much faster print(system.time(f1())) print(system.time(f2()))