[R] generate random numbers for lotteries

Mike Miller mbmiller+l at gmail.com
Mon Apr 30 02:21:25 CEST 2012


On Mon, 30 Apr 2012, Vale Fara wrote:

> ok, what to do is to generate two sets (x,y) of integer uniform random 
> numbers so that the following condition is satisfied: the sum of the 
> numbers obtained in x,y matched two by two (first number obtained in x 
> with first number obtained in y and so on) is in mean equal to a value 
> z, mean value that I can decide before the randomization.
>
> Hope this is more clear than before...

It isn't very clear to me.  If you generate random X,Y pairs such that 
(X+Y)/2=z, then you have a only one random number and a nother that is 
completely dependent on it:

X = random
Y = 2z - X.

I'll just tell you one thing you might be able to use, but I don't have 
time for this.

To make a vector of N uniformly-distributed random integers in the range 
from integer A to integer B, inclusive, you can do this:

floor( runif(N, min=A, max=B+1) )

The floor() function rounds down to the nearest integer.  Depending on the 
exact nature of the algorithm, it might be possible for B+1 to happen, but 
it would be extremely unlikely, if it really is possible.

This should do the same thing:

floor((B-A+1)*runif(N)+A)

The ceiling function can accomplish the same thing.  To make random 
integers from 1 to K, do this:

ceiling( K*runif(N) )

Mike



More information about the R-help mailing list