Generating random numbers from the Laplace distribution

r, statistics

Solution

From the Probability distributions CRAN Task View, there are several packages that already implement the Laplace distribution, notably distr and Runuran.

So you should be able to install `distr`, for example, and do something like :

library(distr)
D <- DExp(rate = 1) 
r(D)(1)

Code taken from the examples of the `DExp-class` help page.

Problem

I have been trying to generate random numbers from the double exponential(Laplace) distribution. I am at a point I can write the code anymore. Any help would be appreciated. The code below is what I have written. ``` rlaplace = function(u,a,b){ u = c(runif(ns)) for(i in 1:ns){ if(u[i] <= 0.5){ X = a+b*log(2*u) } else{ X = a-b*log(2*(1-u)) } } X } z1 = rlaplace(u,a,b) ```

Original source