Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Perl - Unique Numbers (algorithm)

by tye (Sage)
on Oct 19, 2015 at 14:37 UTC ( [id://1145335]=note: print w/replies, xml ) Need Help??


in reply to Perl - Unique Numbers

Heh, the algorithmically optimal (IMHO) way to get N unique numbers from a set of M of them is not actually covered in any of the replies. Not that this means you shouldn't use any of those solutions. I just think the method should be mentioned.

The process is the same as that used in List::Util's shuffle(), the Fisher-Yates Shuffle, except you stop early.

sub tenOf20 { my @set = 1..20; for( 0..9 ) { my $r = $_ + int( rand(@set-$_) ); @set[$_,$r] = @set[$r,$_] if $r != $_; } return @set[0..9]; }

- tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-20 00:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found