in reply to Picking unique element in random from a array
#!/usr/bin/perl my $n = 1000; my $cnt = 10; my %ret; for (my $i = 0; $i < $cnt; $i++) { my $r = int(rand()*$n); $r++ while exists $ret{$r}; $ret{$r} = undef; $n--; } print "$cnt random indexes:\n"; foreach (keys %ret) { print "$_\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Picking unique element in random from a array
by pajout (Curate) on Aug 10, 2006 at 13:19 UTC |