No such thing as a small change PerlMonks

### Re: unique random numbers

by BrowserUk (Pope)
 on Sep 17, 2002 at 07:10 UTC ( #198436=note: print w/replies, xml ) Need Help??

in reply to unique random numbers

There is no need to use hashes at all. Just shuffle your list of available numbers then move them in chunks of 10 (or whatever) to a list of lists using splice. Then just pop one off when you need it.

```#! perl -sw
use strict;
# get available numbers from somewhere.
my @available = (100..999);

# shuffle them
\$a = \$_ + rand @available - \$_ and @available[\$_, \$a] = @available[\$a,
+ \$_] for (0..\$#available);
my @lists;

# move the randomly distributed numbers into 10 lists 10 at a time.
\$lists[\$_] = [ splice( @available, 0, 10) ] for 1..10;

# to use and discard the next number from the 5th list
my \$next = pop @{\$lists[5]};

print "list[\$_]=( @{\$lists[\$_]} )\n" for 1..10;

__END__
# Output Note: The number used and discarded from the 5th list.
C:\test>198405.pl
C:\test>198405.pl
list[1]=( 473 849 328 853 535 166 196 138 466 553 )
list[2]=( 998 533 640 693 564 555 498 766 614 294 )
list[3]=( 527 195 722 886 751 458 134 186 246 556 )
list[4]=( 461 120 694 440 387 221 938 862 971 439 )
list[5]=( 457 957 446 976 397 403 425 748 966 )
list[6]=( 870 877 139 776 496 271 378 811 565 472 )
list[7]=( 942 414 463 358 146 958 168 728 158 338 )
list[8]=( 162 169 509 268 815 716 410 629 965 705 )
list[9]=( 842 468 203 412 651 130 343 539 219 177 )
list[10]=( 734 224 275 658 127 810 874 649 844 633 )

C:\test>
[download]```

Well It's better than the Abottoire, but Yorkshire!

Log In?
 Username: Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://198436]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2020-12-02 18:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How often do you use taint mode?

Results (44 votes). Check out past polls.

Notices?