What's a clear, short verb to select random items in a weighted fashion?
Something like ? only:
#0 Ranks 1 0 1
#1 y=.vector of weights (proportional to their total)
#2 x=.number of trials
#3 the monad is the dyad with a fixed left arg of 1, i.e. 1&$: :
( ... )
#4 result is x indexes into y (i.e. results e. i.#y) , selected
at random, weighted by the index's proportion of the total
Something like:
require'stats numeric'
wProb =: 1&$: :( discreterand@:(;~ ] ,:~ i.@# ) )"1 0 1
] results =. 4e5 wProb Y=.5 10 25 60
3 0 3 2 2 3 2 1 3 3 3 3 3 2 3 3 1 2 3 3 3 2 3 3 3 2 0 3 3 3 3 3 3
...
Y ,:~ (1 round 100 * #/.~ % #) (i.n=.#Y) , results
5 10 25 60
5 10 25 60
Only discreterand seems a little heavyweight. Is there a simpler way?
-Dan
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm