in reply to Picking unique element in random from a array

A slight change to your code can help:

my %seen; for my $x (0..9) { my $element = $quest[rand @quest]; redo if $seen{$element}++; print $element; }

You set the element as a key to the hash, and you test to see if you have already used that element.

--
Leviathan

Replies are listed 'Best First'.
Re^2: Picking unique element in random from a array
by Limbic~Region (Chancellor) on Aug 09, 2006 at 13:29 UTC
    Leviathan,
    While this code works, it can be terribly runtime inefficient. Imagine that you have an initial data set of 10_000 elements and the desired random set is 9_990 elements. This is how Data::Random does it under the covers and I have emailed the author with a couple of alternatives.

    Cheers - L~R