in reply to Efficiency & Maintenance: if/elsif or Other Dispatch Method
A solution that might lack elegance but should be pretty fast is a simple hash look-up. Construct a hash with keys running from 1 .. 100 and values equivalent to the 'probability' for each key. Something like this:
my $outcome = shift; my %h = map { $_ => 10 * int ( ( $_ + 4 ) /10 ) } 8 .. 93; @h{1 .. 3} = (0) x 3; @h{4 .. 7} = (5) x 5; @h{94 .. 97} = (95) x 4; @h{98 .. 100} = (99) x 3; print defined $h{$outcome} ? sprintf "%02d\n", $h{$outcome} : 'ERROR';
Update: Just realised that your numbers don't have to be integers. If so, change the beginning of the above to:
use POSIX; my $outcome = ceil( shift );
|
|---|