[Haskell-cafe] Strange random choice algorithm

michael rice nowgate at yahoo.com
Sat Jan 30 19:28:08 EST 2010


Hi all,

Very nice analysis, Sebastian.

Thanks,

Michael

--- On Sat, 1/30/10, Sebastian Fischer <sebf at informatik.uni-kiel.de> wrote:

From: Sebastian Fischer <sebf at informatik.uni-kiel.de>
Subject: Re: [Haskell-cafe] Strange random choice algorithm
To: "haskell-cafe Cafe" <haskell-cafe at haskell.org>
Date: Saturday, January 30, 2010, 6:06 PM


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
Haskell-Cafe at haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100130/d4a47120/attachment.html


More information about the Haskell-Cafe mailing list