In some simulation code I am working on I need to generate a random set of percentages for various components. The sum of the individual percentages for the components must be 100, but there are constraints on the minimum and maximum percentage for each component.

The current code for generating simulation sets looks somewhat like that shown below.

use strict; use warnings; my @constraints = ( {mid => 20, sd => 15}, {mid => 30, sd => 25}, {mid => 50, sd => 10}, ); my $sum; for my $cnst (@constraints) { my $value = RandFlat ($cnst->{mid}, $cnst->{sd}); $cnst->{value} = $value; $sum += $value; } #scale so that percentages add up to 100 # this may push parameters outside bounds! my $scale = 100.0 / $sum; for my $cnst (@constraints) { my ($mid, $sd, $value) = @{$cnst}{'mid', 'sd', 'value'}; $value = $value * $scale; printf "%5.1f +-%5.1f: %5.1f", $mid, $sd, $value; print " - bad" if ($value < ($mid - $sd)) || ($value > ($mid + $s +d)); print "\n"; } sub RandFlat { #Return a rand () value with a flat distribution about the $mean + +- $stdDev my ($mean, $stdDev) = @_; my $range = 2.0 * $stdDev; my $value = rand ($range); return $value + ($mean-$stdDev); }

Prints:

20.0 +- 15.0: 22.7 30.0 +- 25.0: 40.1 50.0 +- 10.0: 37.2 - bad

However this technique can generate sets that do not conform to the constraints (as shown). Depending on the actual constraints the probability of generating a compliant data set may be rather low! Is there a better technique for solving this problem?

Notes:


DWIM is Perl's answer to Gödel

In reply to Need technique for generating constrained random data sets by GrandFather

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.