"be consistent" | |
PerlMonks |
Re: Idiom to return 0 or random number of array elementsby davido (Cardinal) |
on Dec 07, 2012 at 06:18 UTC ( [id://1007692]=note: print w/replies, xml ) | Need Help?? |
LanX got to the @slice[ 0 .. -1 ] solution ahead of me, but I'm thinking you might still be missing something. In your first example, "$element = $array[int rand @array];", you're selecting a random element. In your second example, "@elements = @array[0 .. int rand @array];" you're selecting a random number of elements, but the elements being selected are not random except for the quantity. If your array contains "a, b, c, d, e", you'll get "a", or "a,b", or "a,b,c", etc. Even after dealing with the ability to get no elements at all, you're still not actually getting random elements. Maybe that's what you want. Or maybe you want a random number of random elements (with no duplicates). If that's the case, you might just shuffle:
Dave
In Section
Seekers of Perl Wisdom
|
|