set.seed(123)
# Step 1: generate Uniform(0,1) values
u <- runif(1000)
# Step 2: transform them into Exponential(lambda)
lambda <- 2 # rate parameter
x <- -log(1 - u) / lambda
# Step 3: Plot and compare with R's built-in rexp()
x_builtin <- rexp(1000, rate = lambda)
hist(x, breaks = 30, col = rgb(0,0,1,0.4), freq = FALSE,
main = "Comparison of PDF of exponential distribution: Transform vs rexp()")
hist(x_builtin, breaks = 30, col = rgb(1,0,0,0.4), freq = FALSE, add = TRUE)
legend("topright", legend = c("Transform", "rexp()"),
fill = c(rgb(0,0,1,0.4), rgb(1,0,0,0.4)))