Simulation of exponential distribution

Suppose I have an exponential distribution with rate 0.5 and n=1000 together with a uniform distribution between 0 and 1. And I also need to draw a histogram. Write the R code.
The R code I wrote is :

Just wanna know whether my R code is correct? Do I need to perform the inversion method here?

Well since you have mentioned 'together' with uniform distribution, I think it would be more appropriate to go with inverse transform sampling as normalization is required to work out the inverse CDF.

numSamples <- 1000
lambdaRate <- 0.5
U <- runif(numSamples, min = 0, max = 1)
X <- -log(1-U)/lambdaRate

# plot
hist(X, freq=F, xlab='X', main='graph')
  dexp(x, rate = lambdaRate), 
  from = 0, to = 11, lwd=2, xlab = "", ylab = "", add = T


thanks !!!! i think i get it

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.