[R] generate a random number with rexp ?

Petr Savicky savicky at cs.cas.cz
Fri Jan 27 12:28:54 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.

Let me call the bounds low and high. Restricting the 
output to the interval [low, high] may be done by
rejection method. For example, as follows.

  x <- low + rexp(n)
  y <- x[x <= high]

If the interval is short, you get much fewer cases
in y than n, so n should be adjusted accordingly.

Hope this helps.

Petr Savicky.



More information about the R-help mailing list