X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=src%2FMisc.pl;h=c8a06371df23e0f14369406c9eebd54ec29547c4;hb=34fd2bb479299618074fc3b7716eba026f287653;hp=e580fa0ece969f578dec4a5059007aebb514727f;hpb=eb6c2c4db5e4384a3a16a26a1046e6ea4df8ed75;p=infobot.git diff --git a/src/Misc.pl b/src/Misc.pl index e580fa0..c8a0637 100644 --- a/src/Misc.pl +++ b/src/Misc.pl @@ -367,36 +367,28 @@ sub getRandom { return $array[int(rand(scalar @array))]; } -# Usage: &getRandomInt("30-60"); +# Usage: &getRandomInt("30-60"); &getRandomInt(5); +# Desc : Returns a randomn integer between "X-Y" or 1 and the value passed sub getRandomInt { - my $str = $_[0]; + my $str = shift; - if (!defined $str) { - &WARN("gRI: str == NULL."); - return; - } - - srand(); - - if ($str =~ /^(\d+(\.\d+)?)$/) { - my $i = $1; - my $fuzzy = int(rand 5); - if ($i < 10) { - return $i; + if ( !defined $str ) { + &WARN("getRandomInt: str == NULL."); + return undef; } - if (rand > 0.5) { - return ($i - $fuzzy)*60; + + if ( $str =~ /^(\d+(\.\d+)?)$/ ) { + return int( rand $str ) + 1; + } elsif ( $str =~ /^(\d+)-(\d+)$/ ) { + return $1 if $1 == $2; + my $min = $1 < $2 ? $1 : $2; # Swap is backwords + my $max = $2 > $1 ? $2 : $1; + return int( rand( $max - $min + 1 ) ) + $min; } else { - return ($i + $fuzzy)*60; - } - } elsif ($str =~ /^(\d+)-(\d+)$/) { - return ($2 - $1)*int(rand $1)*60; - } else { - return $str; # hope we're safe. - } - &ERROR("getRandomInt: invalid arg '$str'."); - return 1800; + # &ERROR("getRandomInt: invalid arg '$str'."); + return undef; + } } ##########