I'm not completely clear on the requirements, but will this do?

           ws            =:  {. [: (\:  ? @: (+ +/) )&:;/ |:

The dyadic verb  ws  is "weighted selection", where  x  is the number of items 
to select (neccesarily  <:#y  ) and  y  is a 2 column table.  The first column 
gives the universe of elements from which to select, the last column is gives 
the weights.  

I'm not a statistician, so in the vernacular: for row  i  and row  j  of y,  
element >(<i;0){y  is  %/;(<(i,j);1){y  times as likely to appear as element  
>(<j;0){y  .  If the universe of elements is numeric,  y  can be open.  
Otherwise it must be boxed (obviously).

Demonstration of use:


           universe      =.  'ABCDEFG'  
           weights       =.  5 2 3 1 1 1 1
        
           y             =.  universe ,.&:(<">) weights
        
           3 ws y
        ACB
           5 ws y
        AFDBG

I can neither define nor define the correctness of  ws  , but this looks pretty 
good:


           stat_test     =.  (10000 # 1) ws"0 _ y
           counts        =.  _1 + #/.~ universe , ,stat_test
           1 round (% <./) counts % # stat_test      [require 'numeric'
        5 2 3 1 1 1 1


I hope this helps.

-Dan

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to