BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:
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:
They could all end up clustered at one end, or the other, or bunched up in the middle some where.
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Random parttitions?
by hdb (Monsignor) on May 03, 2015 at 12:49 UTC | |
|
Re: Random parttitions?
by RichardK (Parson) on May 03, 2015 at 13:16 UTC | |
by hdb (Monsignor) on May 03, 2015 at 14:07 UTC | |
|
Re: Random partitions? (Thanks and solution.)
by BrowserUk (Patriarch) on May 04, 2015 at 00:15 UTC | |
by GrandFather (Saint) on May 04, 2015 at 04:22 UTC | |
by BrowserUk (Patriarch) on May 04, 2015 at 07:19 UTC | |
|
Re: Random parttitions?
by pme (Monsignor) on May 03, 2015 at 18:44 UTC | |
|
Re: Random partitions?
by QM (Parson) on May 05, 2015 at 08:37 UTC | |
by BrowserUk (Patriarch) on May 05, 2015 at 10:35 UTC |