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
In reply to Re: Trial and error problems in Perl
by BrowserUk
in thread Trial and error problems in Perl
by dbdiaz
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |