#!/usr/bin/perl -w use strict; srand; my $numtimes = 6; for ( my $rollattempt = 0 ; $rollattempt < $numtimes ; $rollattempt++ ) { my $numdice = 4; my $numsides = 6; my $sumof = 3; my $highest = 1; my @excludes = (1); print( "Roll attempt: ", $rollattempt, "\n", "\t", "Sum: ", &exclude_and_sum( $numdice, $numsides, $sumof, $highest, @excludes ), "\n" ); } sub exclude_and_sum { my ( $nd, $ns, $so, $high, @excl ) = @_; my @droll = (); my @temproll = (); my $sum = 0; for ( my $dice = 0 ; $dice < $nd ; $dice++ ) { push ( @droll, int( rand($ns) + 1 ) ); } @temproll = sort(@droll); @droll = (); while ( my $d = pop (@temproll) ) { $d = int( rand($ns) + 1 ) if ( scalar( grep( $d, @excl ) ) ); push ( @droll, $d ); } if ($high) { @droll = sort(@droll); } else { @droll = sort( { $b <=> $a } @droll ); } for ( my $count = 0 ; $count < $so ; $count++ ) { $sum += pop (@droll); } return ($sum); }