[R] generate a random number with rexp ?

Petr Savicky savicky at cs.cas.cz
Fri Jan 27 12:47:05 CET 2012


On Fri, Jan 27, 2012 at 11:49:13AM +0100, Adel ESSAFI wrote:
> dear list
> 
> I use runif to generate a ramdom number between min and max
> 
> runif(n, min=0, max=1)
> 
> 
> however , the syntaxe of rexp does not allow that
> 
> rexp(n, rate = 1)
> 
> 
> and it generate a number with the corresponding rate.
> 
> The question is: how to generate a number between min and max using rexp().

Hi.

Alternatively, the inverse distribution function

  g <- function(x, lambda) { - 1/lambda * log(1 - x) }
  y <- g(runif(n, min=pexp(low, rate=lambda), max=pexp(high, rate=lambda)), lambda)

may be used. In this case, the length of the output is n.

Hope this helps.

Petr Savicky.



More information about the R-help mailing list