[email protected]
[Top] [All Lists]

Re: [Haskell-cafe] Strange random choice algorithm

Subject: Re: [Haskell-cafe] Strange random choice algorithm
From: Sebastian Fischer
Date: Sun, 31 Jan 2010 00:06:20 +0100
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.
Anyway,

    pick xs = do n <- randomRIO (1,length xs)
                 return (xs!!(n-1))

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 time-honored tradition.
(D.G.)



_______________________________________________
Haskell-Cafe mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/haskell-cafe

<Prev in Thread] Current Thread [Next in Thread>