[R] generate random numbers for lotteries

R. Michael Weylandt michael.weylandt at gmail.com
Mon Apr 30 00:07:20 CEST 2012


On Sun, Apr 29, 2012 at 4:38 PM, Vale Fara <valef78 at gmail.com> wrote:
> Hi,
>
> thank you both for your replies, I really appreciate it!
>
> To Mike: yes, random integers. Can I use the function round() as in
> the example with 5 random numbers below?
>
> To Billy: for the second part I got an error, but it may be that I
> didn't properly set "i"...?
> Here is the R output:
> x <- runif(60 , 0 , 10) # 60 numbers from 0 to 10
>> x
>  [1] 3.29208009 8.62643979 3.40762875 0.35224714 6.78760173 4.72160818 etc
> y<-  runif(60, 15 , 25) # same as above , from 15 to 25
>> y
>  [1] 18.48391 24.44361 23.20955 16.61257 23.06074 17.71158 23.67585 19.31675 etc
> i <- 12
>> i
> [1] 12
>> for( i in 1:length(x)) {
> + z = x[i] + y[i]
> + return z}
> Errore: unexpected symbol in:
> "z = x[i] + y[i]
> return z"

I noted here (http://tolstoy.newcastle.edu.au/R/e17/help/12/04/11923.html)
but I think it got caught by the spam filters (for whatever reason)
that this is invalid code and you simply need:

z <- x + y

and the magic of vectorization will save the day.

Michael

>
> To all: I attach an example with only 5 random numbers
>
> step 1. random number generation
> x <- round(runif(5 , 0 , 10) )
> < x
>  [1]  2 1  0  6  8
> y <- round(runif(5 , 15 , 25) )
>> y
>  [1] 21 24 22 22 19
>
> step2. matching of numbers from x and y to create lotteries
> lottery 1: 2$ with 50% probability vs.  21$ with 50% of probability
> (Expected value: 2*0.5+21*0.5=11.5)
> lottery 2: 1$ with 50% probability vs.  24$ with 50% of probability
> (Expected value: 1*0.5+24*0.5=12.5)
> lottery 3: 0$ with 50% probability vs.  22$ with 50% of probability
> (Expected value: 0*0.5+22*0.5=11)
> lottery 4: 6$ with 50% probability vs.  22$ with 50% of probability
> (Expected value: 6*0.5+22*0.5=14)
> lottery 5: 8$ with 50% probability vs.  19$ with 50% of probability
> (Expected value: 8*0.5+19*0.5=13.5)
>
> step 3. Form step 2, I know that the min EV of these lotteries is 11
> and the max EV is 14.
> The difference is: max EV - min EV = 3.
> I need a difference of 5
>
> How to do this?
>
> Valeria
>
>
>
> 2012/4/29 billy am <wickedpuppy at gmail.com>:
>> Interesting set of question.. I am completely new to R but let me try my
>> luck.
>>
>> Random number in R
>>
>> x <- runif(60 , 0 , 10) # 60 numbers from 0 to 10
>> y<-  runif(60, 15 , 25) # same as above , from 15 to 25
>>
>> The second part though. Do you mean ,
>>
>> for( i in 1:length(x)) {
>> z = x[i] + y[i]
>> return z
>> }
>>
>> something like that? No idea about the third part though.
>>
>> regards
>> Billy
>>
>> On Sun, Apr 29, 2012 at 9:29 AM, Mike Miller <mbmiller+l at gmail.com> wrote:
>>>
>>> On Fri, 27 Apr 2012, Vale Fara wrote:
>>>
>>>> I am working with lotteries and I need to generate two sets of uniform
>>>> random numbers.
>>>>
>>>> Requirements:
>>>> 1) each set has 60 random numbers
>>>
>>>
>>> random integers?
>>>
>>>
>>>> 2) random numbers in the first set are taken from an interval (0-10),
>>>> whereas numbers in the second set are taken from a higher interval
>>>> (15-25)
>>>
>>>
>>> Depends on if you mean integers.  R has functions.  Here's one:
>>>
>>> http://www.astrostatistics.psu.edu/su07/R/html/stats/html/Uniform.html
>>>
>>>
>>>
>>>> 3) numbers generated in the first set should be matched to numbers in
>>>> the second set (row by row) so that the expected value of each couple
>>>> of random numbers (i.e. of each lottery) is around to a given value
>>>> (12.5 +/- 5, where 12.5 is the median value of the interval extremes).
>>>
>>>
>>> Do you mean that the mean for the pair of numbers must be between 7.5 and
>>> 17.5, inclusive?  That means the sum must be from 15 to 35.  Well, you are
>>> in luck because if you make the numbers as you suggested above, that will
>>> happen -- you don't have to do anything special to make it happen.
>>>
>>>
>>>
>>>> For the computation of the expected value, the probabilities in each
>>>> lottery are ½ and ½.
>>>
>>>
>>> For what outcome?  You lost me.
>>>
>>>
>>>
>>>> How do this? Any help given would be greatly appreciated.
>>>
>>>
>>> I hope that helps.
>>>
>>> Mike
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>>
>>
>> --
>> http://use-r.com  - R Statistics Scripting & Programming Language Online
>> Community Home
>> http://bam.domainpool.com - Domains For Sale
>> http://SQLHack.net - Tech Blog/Tutorials
>>
>> ------------------------------------------------------------------------------------
>>
>> MCSA (Security) , MCSA , CCNA , LPIC - Lvl 1 , Security+ ,  Network+ , A+
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list