[Haskell-beginners] Re: [Haskell-cafe] Re: permuting a list

Alberto Ruiz aruiz at um.es
Mon Feb 16 04:24:59 EST 2009


Heinrich Apfelmus wrote:
> Alberto Ruiz wrote:
>> How about using random doubles?
>>
>> randomPerm xs = fmap (map snd . sort . flip zip xs) rs
>>     where rs = fmap (randoms . mkStdGen) randomIO :: IO [Double]
> 
> Interesting idea. The chance of duplicates should be negligible now, but
> that's because we're using a large amount of random bits, far more than
> n! would require.

Another possibility is using infinite lists of random bits as keys. Then 
we only extract from the random number generator the number of bits 
required to avoid duplicates.

-Alberto


More information about the Beginners mailing list