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

Harald Bögeholz bo at ct.de
Wed Sep 26 09:33:26 CEST 2012


Hi,


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.


Thanks
-- 
Harald Bögeholz    <bo at ct.de> (PGP key available from servers)
Redaktion c't      Tel.: +49 511 5352-300  Fax: +49 511 5352-417
                   http://www.ct.de/

                   int f[9814],b,c=9814,g,i;long a=1e4,d,e,h;
                   main(){for(;b=c,c-=14;i=printf("%04d",e+d/a),e=d%a)
                   while(g=--b*2)d=h*b+a*(i?f[b]:a/5),h=d/--g,f[b]=d%g;}
                                                          (Arndt/Haenel)

                   Affe Apfel Vergaser

/* Heise Zeitschriften Verlag GmbH & Co. KG * Karl-Wiechert-Allee 10 *
   30625 Hannover * Registergericht: Amtsgericht Hannover HRA 26709 *
   Persönlich haftende Gesellschafterin: Heise Zeitschriften Verlag *
   Geschäftsführung GmbH * Registergericht: Amtsgericht Hannover, HRB
   60405 * Geschäftsführer: Ansgar Heise, Dr. Alfons Schräder */



More information about the Beginners mailing list