[R] generate a random number with rexp ?

(Ted Harding) Ted.Harding at wlandres.net
Fri Jan 27 12:48:29 CET 2012


On 27-Jan-2012 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().
> 
> Regards
> -- 
> PhD candidate in Computer Science
> Address
> 3 avenue lamine, cité ezzahra, Sousse 4000
> Tunisia
> tel: +216 97 246 706 (+33640302046 jusqu'au 15/6)
> fax: +216 71 391 166

Your question need to give more detail in order to be
well defined!

Since the range of an exponentially distributed random number
is [0,Inf], in the first instance it makes no sense to seek
one distributed over a finite range (min,max) -- unlike a
uniformly distributed random number which does have a finite
range.

However, one possible interpretation of your question would
be a *truncated* exponential distribution, i.e. the distribution
of an exponentially distributed number *conditional on being
between min and max*.

For an exponentially distributed random variable X with rate r,
exp(-r*X) is uniformaly distributed on (0,1).

Conditional on being between min=A and max=B, exp(-r*X)
is uniformaly distributed on (exp(-r*B), exp(-r*A).
Let C = exp(-r*B), D = exp(r*A)
Hence sample U according to runif(n,min=C,max=D) and then

  X <- (1/r)*log(1/U)

will have the exponential distribution truncated to (A,B).

For example:

  A <- 2 ; B <- 11
  r <- 0.25
  C <- exp(-r*B); D <- exp(-r*A)
  n <- 10000
  U <- runif(n,min=C,max=D)
  X <- (1/r)*log(1/U)
  hist(X,breaks=100,xlim=c(0,12))

However, you may have some other meaning in mind for
"variable exponentially distributed with min=A and mex=B".
We would need to knpow!

Ted.

-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 27-Jan-2012  Time: 11:48:14
This message was sent by XFMail



More information about the R-help mailing list