
On Jan 30, 2010, at 8:59 PM, michael rice wrote:
I'm not sure where I got this PICK function from, and don't
understand why it's written as it is, so I wanted to test it for
randomness. It seems random enough.
We can convince ourselves using reason instead of tests. An element is
selected by the algorithm if it is picked and no later element is
picked afterwards. It doesn't matter which elements are picked before.
The nth element of the given list replaces the current selection with
probability (1/n). Hence, the probability that the nth element is
selected in the end is (1/n)*(11/(n+1))*...*(11/m) if there are m
elements. For example, if there are 9 elements, the probability of
selecting the 7th is 1/7 * 7/8 * 8/9 which is 1/9 because the 7 and 8
are canceled out. This happens for all elements and, thus, every
element is selected with probability 1/9.
Anyway,
pick xs = do n < randomRIO (1,length xs)
return (xs!!(n1))
would have been clearer.. It queries the random number generator only
once but walks through the list twice.
Sebastian

Underestimating the novelty of the future is a timehonored tradition.
(D.G.)
_______________________________________________
HaskellCafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskellcafe

