I have to sample a certain number of objects from a population. The following piece of code gives the sampling with replacement. What condition can I use for the sampling without replacement?
do while(j .lt. w)
call random_number(u1)
j1 = 1 + int(population*u1)
Z = inftime(j1)
Z1 = X(j1)
Z2 = Y(j1)
do t = 1, 10
if(Z.gt.0 .and. Z.le.10)then
if(t==Z .and.( my_cnt(t) .lt. k1(t)))then
my_cnt(t) = my_cnt(t) + 1
j = j+1
inftime1(j)= Z
X1(j) = Z1
Y1(j) = z2
endif
endif
enddo
enddo
Thanks in advance
If I understand correctly you use the statements
to generate an integer index (
j1) to select the j1-th element of a population. I’m going to call that the i-th element out of a population of n. At each iteration of the loop you generate a new random number and select a new i-th element, with some probability that it is the same element as you have chosen in one (or more) previous iterations. You’d rather have an approach that avoids re-selecting an already-selected i. I think you have at least 3 approaches.One: Keep a list of the i‘s you’ve already used. If the random number generator throws up the same i in a later iteration, throw the dice again until you get a new one. This requires a little straightforward book-keeping. If you need help programming this, edit your question. This method begins to lose its appeal as the proportion of already-selected i‘s grows large. One you’ve selected 50% of the members of the population you’ll have to roll the dice twice (on average) for each new i. You can decide whether or not this is a serious issue for you.
Two: Create a random permutation of the populations; in the first iteration choose the first of the populations (in their new random order), in the second the second, and so on. Of course you wouldn’t really permute the populations, you’d create a random permutation of the indices into the populations. This Wikipedia article suggests how you might create such a random permutation. Again, if you have trouble coding this, edit your question, or ask a new one.
Three: This is a variation on Two and is what I would do. First, outside the loop, create an array of n random numbers:
Next, inside the loop, find the index of the smallest value in the array:
Note that I’ve gone back to your notation and used
j1as the index for the population. Note also the specification ofdim=1on the call tominlocto ensure that it returns a scalar: I think this is Fortran 2003 semantics and earlier implementations may return a rank-1 array with 1 element so check your compiler documentation.If
minlocfinds 2 (or more) elements in the array with the same value, relatively unlikely since they arereals, it just returns the index of the first one it finds, which is OK for our purposes.Next, to ensure that we don’t choose the same index at the next iteration, do this:
taking care to ensure that the argument to the call to
hugeis of the same type and kind as therealnumbers in therandom_array.This should enumerate the elements of the population randomly without replacement.