in reply to Remove lowest number
There's a much better way of doing what you're doing.
This code has been tested relatively thoroughly. You should be able to just drop the functions into your existing code, or run it separately as provided.sub roll_die { my ($sides, $min) = @_; return int(rand($sides - $min + 1) + $min); } sub my_roller { my ($num_dice, $sides, $min_value, $num_to_drop) = @_; $num_dice ||= 3; $min_value ||= 1; $num_to_drop ||= 0; return () unless $num_dice >= 1; return () unless $num_dice > $num_to_drop; my @values = sort { $a <=> $b } map { roll_die($sides, $min_value) } 1 .. $num_dice; return @values[$num_to_drop .. $#values]; } my @values = my_roller(4, 6, 2, 1); print "@values\n";
Improvements made over what you posted:
------
We are the carpenters and bricklayers of the Information Age.
The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6
Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.
|
|---|