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 n-th element of the given list replaces the current selection with
probability (1/n). Hence, the probability that the n-th element is
selected in the end is (1/n)*(1-1/(n+1))*...*(1-1/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.
pick xs = do n <- randomRIO (1,length xs)
would have been clearer.. It queries the random number generator only
once but walks through the list twice.
Underestimating the novelty of the future is a time-honored tradition.
Haskell-Cafe mailing list