[Haskell-beginners] Picking a random element from a Map

Daniel Trstenjak daniel.trstenjak at gmail.com
Wed Sep 26 10:09:28 CEST 2012

Hi Harald,

> how do I (quickly) pick a random element from a `Data.Map.Map`?
> Right now I am using `head $ Map.keys p` to get the first element, but
> I'd like to replace this by a random element.
> I know I have to drag around the state of the random generator
> somewhere, but if I have one I am wondering what's the best way to use
> one to take a pick from the map.

M.keys map !! randomValue `mod` M.size map

But if you need a high performance solution than it's more appropriate
to put the values into a Data.Vector and indexing into it.


More information about the Beginners mailing list