[R] Generate quasi-random positive numbers

(Ted Harding) Ted.Harding at wlandres.net
Tue Aug 5 12:46:55 CEST 2014


On 05-Aug-2014 10:27:54 Frederico Mestre wrote:
> Hello all:
> 
> Is it possible to generate quasi-random positive numbers, given a standard
> deviation and mean? I need all positive values to have the same probability
> of selection (uniform distribution). Something like:
> 
> runif(10, min = 0, max = 100)
> 
> This way I'm generating random positive numbers from a uniform
> distribution. However, using runif I can't previously select SD and mean
> (as in rnorm).
> 
> Alternatively, I'm able to generate a list of quasi-random numbers given a
> SD and a mean.
> 
> b <- (sqrt(SD^2*12)+(MEAN*2))/2
> a <- (MEAN*2) - b
> x1 <- runif(N,a,b)
> 
> However, negative values might be included, since "a" can assume a negative
> value.
> 
> Any help?
> 
> Thanks,
> Frederico

There is an inevitable constraint on MEAN and SD for a uniform
ditribution of positive numbers. Say the parent distribution is
uniform on (a,b) with a >= 0 and b > a.

Then MEAN = (a+b)/2, SD^2 = ((b-a)^2)/12, so

  12*SD^2  = b^2 - 2*a*b + a^2
  4*MEAN^2 = b^2 + 2*a*b + a^2

  4*MEAN^2 - 12*SD^2 = 4*a*b

  MEAN^2 - 3*SD^2 = a*b

Hence for a >= 0 and b > a you must have MEAN^2 >= 3*SD^2.

Once you have MEAN and SD satisfying this constraint, you should
be able to solve the equations for a and b.

Hoping this helps,
Ted.

-------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at wlandres.net>
Date: 05-Aug-2014  Time: 11:46:52
This message was sent by XFMail



More information about the R-help mailing list