[R] Pseudo-random numbers between two numbers

Duncan Murdoch murdoch at stats.uwo.ca
Wed Mar 11 11:57:02 CET 2009


guox at ucalgary.ca wrote:
> Please forget the last email I sent with the same subject.
> =================
> I would like to generate pseudo-random numbers between two numbers using
> R, up to a given distribution,
> for instance, norm. That is something like rnorm(HowMany,Min,Max,mean,sd)
> over rnorm(HowMany,mean,sd).
> I am wondering if
>
> qnorm(runif(HowMany, pnorm(Min,mean,sd), pnorm(Max,mean, sd)), mean, sd)
>
> is good.


It depends on what Min and Max are.  If you get far out in the tails, 
rounding error will kill you.  For example, pnorm(x) is exactly 1 for x 
bigger than 10 or so, so this approach would fail if Min and Max were 
both bigger than 10.  The solution is to switch to lower=FALSE in the 
upper tail, and possibly switch to a log scale if you want to be really 
extreme.

Duncan Murdoch




More information about the R-help mailing list