use Math::Random::MT qw[ srand rand ]; use POSIX qw[ frexp ]; sub bigRand { my $max = shift; my $exp = scalar frexp( $max ); ++$exp if $exp &1; ## Update: Didn't work if exponent was odd. my $halfbits = $exp / 2; my @rand = map{ rand 2**$halfbits } 1 .. 2; my $rand = ( $rand[ 0 ] * 2**$halfbits ) + $rand[ 1 ]; ## Updated: Use * not << for floats! my $rv = int( $rand * ( $max / 2**$exp ) ); ## Update: Less roundoff }