# How to simulate exponential random variable Y
# Sample sizes
n = 300
lambda = 1 # Choose lambda for exponential distribution
X = runif(n=n, min=0, max=1) # Sample X from uniform(0, 1)
Y = -1/lambda * log(1-X) # Compute Y using inverse of CDF for an exponential r.v.
title = paste('n =', toString(n))
hist(Y, probability = TRUE, main=title) # histogram of random samples Y
rug(Y) # rug plot
curve(dexp(x, rate=lambda), add=TRUE, col='red') # theoretical density function of y for comparison