mardi 2 février 2016

How do I get a randomly selected hash key in Perl 6?

A quick and hopefully easy question;

I need the means to select from among a given set of hash keys at random. I'll keep poking after this post, but the perl6.org documentation on neither rand nor hash offer many hints.

my %a = 1,2,3,4,5,6;

Given the above,

%a.keys;

returns (5 1 3)... and if I simply try

%a.rand;

However, I get a pseudorandom float rather than any one key.

I eventually cobbled together %a.keys[Int(%a.elems.rand)], but was hoping for something simpler.




Aucun commentaire:

Enregistrer un commentaire