On Jun 19, Jeff 'japhy' Pinyan said:

>  until (keys(%table) == $num_sets) {
>    my %random;
>
>    # until we have the proper number of random numbers
>    until (keys(%random) == $per_set) {
>      # put a random number in the hash
>      $random{ $min + int rand $newmax } = 1;
>    }
>
>    $table{ join " ", sort keys %random } = 1;
>  }

We can eliminate the inner until loop by making it a for loop, and
pregenerating the list of random numbers (not a good idea if it's going to
be very large, though).

  until (keys(%table) == $num_sets) {
    my @random = ($min .. $min + $newmax - 1);
    my @set =
      sort
      map { splice @random, rand @random, 1 }
      1 .. $per_set;
    $table{"@set"} = 1;
  }

This might be less good, though, since splice() can be ugly.  What this is
doing is removing one random number at a time, $per_set times, and then
sorting them.  Because they get removed from @random, they won't repeat.

But maybe that hash idea was better. ;)

-- 
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      **

Reply via email to