Unique random numbers?

cpetenes
cpetenes used Ask the Experts™
on

What's a clean way generate a list of unique random numbers?

All I want is to generate 10 random numbers, say, between 1 and 20 with no repeats.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
#!/usr/bin/perl
for(;;) {
     $r = int(rand 20) + 1;
     if(!$found{$r}) {
          $found{$r}=1;
          $found++;
          if($found >= 10) {
               last;
          }
     }          
}
foreach $value ( keys(%found)) {
     print "$value\n";
}    

Author

Commented:
Thanks!

Much easier than what I was trying to do.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial