Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re^4: Is there a better way to generate unique set of random numbers ?

by happy.barney (Pilgrim)
on Jul 28, 2011 at 10:52 UTC ( #917243=note: print w/ replies, xml ) Need Help??


in reply to Re^3: Is there a better way to generate unique set of random numbers ?
in thread Is there a better way to generate unique set of random numbers ?

use List::Util; sub unique_random_list { my ($from, $to, $count) = @_; $count = List::Util::min ($count, $to - $from); my @result; my @queue = [ $from, $to - $from, $count ]; while (my $job = shift @queue) { my ($from, $length, $count) = @$job; if ($count == $length) { push @result, $from .. $from + $length; } elsif ($count == 1) { push @result, $from + int rand $length; } else { my $split_length = int ($length / 2); my $split_count = List::Util::min (int rand $count, $spli +t_length); my $pad_length = $length - $split_length; my $pad_count = $count - $split_count; $split_count += $pad_count - $pad_length if $pad_count > $pad_length; unshift @queue, grep $_->[2], [ $from, $split_length, $split_count ], [ $from + $split_length, $length - $split_length, $count + - $split_count ]; } } @result; } my @list = unique_random_list (10, 100, 30);


Comment on Re^4: Is there a better way to generate unique set of random numbers ?
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-10-31 08:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (215 votes), past polls