Random Permutations
Andreas Abel
abel@informatik.uni-muenchen.de
Thu, 06 Mar 2003 16:03:17 +0100
>Is there a library routine for random permutations?
>
>I didn't find any and did a quick hack, which works fine for my
>application (length of list < 100), but what would be a more
>elegant way?
Well, sorting is a special case of permuting, so my idea was to use the library
routine
List.sortBy :: (a -> a -> Ordering) -> [a] -> [a]
passing it a comparison function which ignores its arguments and simply returns
a random bit when invoked, e.g.
permute = sortBy $ \_ _ -> if random() then GT else LT
Unfortunately, this one-line hack is impossible since random numbers rely on
state and sortBy is not prepared for use with a monad.
Back in the old days when we had assignments...
--Andreas
--
Andreas Abel --<>-- What if Jesus is right?
Theoretical Computer Science, University of Munich
http://www.tcs.informatik.uni-muenchen.de/~abel/