[R] generate random numbers for lotteries

Daniel Nordlund djnordlund at frontier.com
Mon Apr 30 03:09:52 CEST 2012


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> On Behalf Of Mike Miller
> Sent: Sunday, April 29, 2012 5:21 PM
> To: Vale Fara
> Cc: r-help at r-project.org; billy am
> Subject: Re: [R] generate random numbers for lotteries
> 
> 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
> 

I don't know what the OP is really trying to accomplish yet, and I am not motivated (yet) to try to figure it out.  However, all this "flooring" and "ceiling) and "rounding" is not necessary for generating uniform random integers.  For N integers in the range from A to B it is as simple as

sample(A:B, N, replace=TRUE)

Hope this is helpful,

Dan


Daniel Nordlund
Bothell, WA USA
 



More information about the R-help mailing list