Given start, end & count, I want to generate an ordered set of count random values between start and end, that partition that range roughly equally. (Not exactly; then they wouldn't be random!)

So, I can do something like this:

sub rndPart{ my( $start, $end, $count ) = @_; sort{ $a <=> $b } map{ sprintf "%.2f", $start + rand( $end - $start ) } 1 .. $count };; print rndPart( 1, 10, 10 ) for 1 .. 10;; 1.26 1.33 2.13 3.69 5.22 5.30 6.16 7.02 9.69 9.98 2.51 3.97 5.80 6.10 6.13 7.30 8.33 8.70 9.87 9.99 3.59 4.94 6.21 6.88 7.23 7.53 8.30 8.36 9.84 9.91 2.97 3.38 4.01 4.81 5.04 5.42 5.99 6.42 8.23 9.06 1.10 2.86 3.12 4.09 4.35 4.38 7.29 7.46 9.02 9.48 1.01 2.70 3.15 3.23 4.47 6.56 7.62 8.84 9.16 9.52 1.08 1.63 3.44 4.22 5.08 5.58 6.34 7.40 7.69 9.40 3.29 3.66 4.44 4.54 4.74 4.81 5.02 5.53 6.53 8.09 1.44 2.34 2.51 4.77 5.74 6.64 8.59 8.80 8.98 9.28

Which isn't too bad, but:

  1. There are no guarantees about the distribution:

    They could all end up clustered at one end, or the other, or bunched up in the middle some where.

  2. The need to sort is a drain on resources if the sets are large.

    It also means that the values are strictly increasing; and whilst I want the general trend to be so; I'd like to allow for the occasional blip above or below.

Is there a better way? Can the sort be avoided? Can the distribution be regularised, with being either rigid; or precluding the occasional skewed set?

I've thought about generating a set of exact partitions, and then applying a randomised delta, but the implementation isn't falling off the page for me today.


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

In reply to Random partitions? by BrowserUk

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.