You're right, I had a thinko.  Thanks for the idea.

Raul, I didn't quite follow the edge condition you mention, can you elaborate?  
Or, in short, is the phrase fairer if -. is used?

-Dan



Please excuse typos; composed on a handheld device.

-----Original Message-----
From: Roger Hui <[email protected]>
Sender: [email protected]
Date: Fri, 27 May 2011 10:22:27 
To: Programming forum<[email protected]>
Reply-To: Programming forum <[email protected]>
Subject: Re: [Jprogramming] Weighted random selection

How is there favoritism?

   b=: 1 1
   (+/\b)%+/b
0.5 1
   +/0 1=/~0.5 1 I. 1e6 ?@$ 0
500380 499620
   +/0 1=/~0.5 1 I. 1e6 ?@$ 0
500866 499134
   +/0 1=/~0.5 1 I. 1e6 ?@$ 0
500429 499571
   +/0 1=/~0.5 1 I. 1e6 ?@$ 0
499958 500042
   +/0 1=/~0.5 1 I. 1e6 ?@$ 0
499918 500082
   +/0 1=/~0.5 1 I. 1e6 ?@$ 0
500454 499546
   +/0 1=/~0.5 1 I. 1e6 ?@$ 0
499724 500276

The coin seems pretty fair to me.



----- Original Message -----
From: Dan Bron <[email protected]>
Date: Friday, May 27, 2011 10:08
Subject: Re: [Jprogramming] Weighted random selection
To: J Programming <[email protected]>

> 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

Reply via email to