Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
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);

In reply to Re^4: Is there a better way to generate unique set of random numbers ? by happy.barney
in thread Is there a better way to generate unique set of random numbers ? by chakreey

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (4)
    As of 2014-09-24 01:50 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (244 votes), past polls