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:
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |