On Jun 19, Scott Taylor said:
>I want to create an array, and populate it with random numbers, they
>should be unique and sorted (I can sort at the output). I'm stuck at
>the unique part:
Whenever you hear "unique", you should think "I should be using a hash."
The same goes for "in" and "duplicate" and "is this the first time I've
seen an element".
That said, use a hash, and stop when the number of keys is the preferred
number.
$limit = 10; # we want 10 random numbers
$random{random_number()} = 1 until keys %random == $limit;
That loop, if it's too concise for you, is:
until (keys(%random) == $limit) {
my $r = random_number();
$random{$r} = 1;
}
Then you extract the keys() of %random.
--
Jeff "japhy" Pinyan [EMAIL PROTECTED] http://www.pobox.com/~japhy/
I am Marillion, the wielder of Ringril, known as Hesinaur, the Winter-Sun.
Are you a Monk? http://www.perlmonks.com/ http://forums.perlguru.com/
Perl Programmer at RiskMetrics Group, Inc. http://www.riskmetrics.com/
Acacia Fraternity, Rensselaer Chapter. Brother #734
** Manning Publications, Co, is publishing my Perl Regex book **