Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: random number -- keno


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: random number -- keno
Date   Wed, 19 Jun 2002 10:20:52 +0100

Benoit Dulong
> 
> Consider n balls:  #1, #2, ..., #n.
> I draw k balls at random, WITHOUT replacement.
> The result is noted x1, x2,...,xk.
> I want to repeat this r times.
> The data file should be:  r lines and k colomns.
> 
> How can I generate this data file?
> Where should I look?
> 
> I looked at STB-28: sg44; STB-41: sg44.1, rnd.
> 
> I am trying to knit my own program... 

I'd generate this as a one long composite sample, 
and then -reshape-. 

Suppose n = 10 , k = 6, r = 100. Then we need n * r 
observations: 

set obs 1000 

We set up ids: 

egen sample = seq(), b(10) 
egen ball = seq(), t(10) 

If desired, 

set seed 123456789 

Get random numbers 

gen random = uniform() 

Shuffle randomly within each sample: 

sort sample random 

We only want 6: 

by sample : keep if _n <= 6 

And -reshape-: 

drop random
by sample : gen id = _n 
reshape wide ball , j(id) i(sample) 


Nick 
n.j.cox@durham.ac.uk 

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index