How to write C code to generate random samples?


utpc
09-11-2002, 05:21 PM
Hello, all

I am a grad in statistics. Following is the question that took me a lot of time and I still cannot get it:

I want to generate random samples from given finite population with known weights. For example, the given finite population is {1,3,5,7}, now I want to draw a random sample from the population with size 500, we are also given that the probabilities of getting 1, 3, 5, 7 are 0.1, 0.4, 0.2, 0.3, respectively. Each of this kind sample contains 500 data points (replications of 1, 3, 5 and 7).

I read the relevant chapters in NR, but cannot get it. Thank you in advance for your kind assistances.

utpc

Kevin Dolan
09-12-2002, 08:35 AM
Use the ran1 or ran2 function to produce random numbers on the interval [0 to 1). Then use the following selection method:

Output of ran1/ran2 selected random number
[0 - 0.1) 1
[0.1 - 0.5) 3
[0.5 - 0.7) 5
[0.7 - 1.0) 7

Note that the notation [a - b) means a number >= a, and < b.