I'm newbee in Fortran and I would like to choose at random a specific variable (specifically its index) by using weights. The weights would be provided in a separate vector (element 1 would contain weight of variable 1 and so on).
I have the following code who does the job without weight (mind being an integer vector with the index of each variable in the original dataset)
call rrand(xrand)
j = int(nn * xrand) + 1
mvar = mind(j)
Thank you for your help!
Here are two examples. The first one is
and the second one is taken from this page
which is essentially the same as the first one. Both sample a random
j
from 1,2,...,5 with weight(j). 100000 trials give a distribution like