in reply to Random partitions?

what about this? no sort and a somewhat adjustable distribution.

sub parts { my ($start,$end,$count,$max) = @_; my $v = $start; for my $i (1..$count) { my $r; do { $r = $v + rand($max); } until $r < $end; $v = $r; printf "%.2f, ",$v; } print "\n"; } for (0..10) { parts(0,10,10,2.5); }
0.90,1.01,1.84,2.11,3.56,3.74,4.57,5.26,6.47,7.00, + + 1.43,1.89,3.00,4.48,6.45,7.86,9.82,9.96,9.96,9.98, + + 0.90,1.58,1.86,2.47,3.34,5.31,5.56,5.75,6.50,8.25, + + 0.90,2.88,3.44,3.93,5.69,7.20,8.74,9.31,9.84,9.89,

It can give you repeated values, particularly at the end. (... 10.0, 10.0) but that's more an issue of rounding than anything else.

Of course if you set the max to large then it could take a long time to finish.

Replies are listed 'Best First'.
Re^2: Random parttitions?
by hdb (Monsignor) on May 03, 2015 at 14:07 UTC

    As an extension of your idea, one could just do $count steps starting from zero, and then map the result to the desired interval.

    use strict; use warnings; sub parts { my ($start, $end, $count) = @_; my @steps = (0); push @steps, rand()+$steps[-1] for 1..$count; my $scale = ($end-$start)/$steps[-1]; $_ *= $scale, $_ += $start for @steps; return @steps; } my @parts = parts 0, 10, 10; print "@parts\n";

    This, of course, is not able to generate skewed distributions, so less flexibility.