i'm working on a perl script and i need a very specific random number generator.
what i have right now is multrand. it returns a random number between x/y and x*y, with the median result being x. half the time less than x, half the time, greater than x.
i want to build a sub that returns a random in the same range, but with the result more likely to be closer to x.
with a simple random, i'd just run the sub z times and return result/z. graph the results of that, and you get a bell curve.
the results/z method won't work here because i still want the results to be half the time less than x and half the time greater than x.
any ideas?
sub multrand { my ( $inmed, $inmult ) = @_; my $randreturn = 0; # $randtemp is used to determine if result will be more than, +less than, or equal to original my $randtemp = int(rand($inmed * 2)); if ( $randtemp > $inmed ) { # $randreturn can be up to $inmed * $inmult $randreturn = int(rand(($inmed*$inmult)-$inmed)) + int +($inmed); } elsif ( $randtemp < $inmed ) { # $randreturn can be as little as $inmed / $inmult $randreturn = int(rand($inmed*(1-(1/$inmult)))) + int( +$inmed*(1/$inmult)); } else { $randreturn = $inmed; } if ( $debug > 2 ) { print "$randreturn\n"; } return $randreturn; }
In reply to custom random number generator by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |