in reply to Trial and error problems in Perl
Generate any three random numbers from any range; sum those numbers; multiply the base number by each of your 3 random numbers in turn and divide by their sum. Result three random numbers that add up to the base number:
Perl> @rands = map{ rand 1000 } 1 .. 3;; Perl> $t += $_ for @rands;; Perl> ( $x1, $x2, $x3 ) = map{ 10.33 * $_ / $t } @rands;; Perl> print "$x1 + $x2 + $x3 = ", $x1 + $x2 + $x3;; 5.13264782081133 + 0.820661299314597 + 4.37669087987407 = 10.33
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Trial and error problems in Perl
by dbdiaz (Novice) on Jan 24, 2007 at 07:25 UTC | |
| A reply falls below the community's threshold of quality. You may see it by logging in. |