Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: custom random number generator
by salva (Canon) on Nov 29, 2011 at 16:17 UTC | |
Re: custom random number generator
by BrowserUk (Patriarch) on Nov 29, 2011 at 18:12 UTC | |
Re: custom random number generator
by JavaFan (Canon) on Nov 29, 2011 at 17:10 UTC | |
Re: custom random number generator
by TJPride (Pilgrim) on Nov 29, 2011 at 20:56 UTC | |
Re: custom random number generator
by toro (Beadle) on Nov 30, 2011 at 07:24 UTC | |
Re: custom random number generator
by BrowserUk (Patriarch) on Nov 29, 2011 at 16:51 UTC |
Back to
Seekers of Perl Wisdom