At first blush, that seems like it would favor higher indices (right-er) over lower ones (left-er)?
-Dan Please excuse typos; composed on a handheld device. -----Original Message----- From: Roger Hui <[email protected]> Sender: [email protected] Date: Fri, 27 May 2011 09:50:06 To: Programming forum<[email protected]> Reply-To: Programming forum <[email protected]> Subject: Re: [Jprogramming] Weighted random selection Convert the weights to cumulative probabilities, then use I. and ?0. Thus: Y=: 5 10 25 60 (+/\Y)%+/Y 0.05 0.15 0.4 1 ((+/\Y)%+/Y) I. 20?@$ 0 2 3 2 3 2 2 3 3 3 1 3 2 1 3 3 3 1 0 1 2 ----- Original Message ----- From: Dan Bron <[email protected]> Date: Friday, May 27, 2011 9:39 Subject: [Jprogramming] Weighted random selection To: 'Programming forum' <[email protected]> > 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? ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
