#! perl -slw use strict; use List::Util qw[ sum ]; sub Nfor (&@) { my( $code, @ranges ) = @_; my @indices = ( 0 ) x @ranges; $indices[ $#indices ]--; my $tumbler = $#indices; my @returns; while( 1 ) { if( ++$indices[ $tumbler ] > $#{ $ranges[ $tumbler ] } ) { $indices[ $tumbler ] = 0; last if --$tumbler < 0; next; } local $SIG{__WARN__} = sub{ warn @_ unless "@_" =~ /Exiting/ }; if( defined wantarray ) { push @returns, $code->( map $ranges[ $_ ][ $indices[ $_ ] ], 0 .. $#indices ); } else { () = $code->( map $ranges[ $_ ][ $indices[ $_ ] ], 0 .. $#indices ); } $tumbler = $#indices; } defined wantarray ? wantarray ? @returns : \@returns : (); } sub genPossibles{ my $constraints = shift; my @possibles; Nfor{ my $sum = sum( @_ ); next if $sum > 100; push @possibles, \@_ if $sum == 100; } map { [ $_->{ mid } - $_->{ sd } .. $_->{ mid } + $_->{ sd } ] } @$constraints; return \@possibles; } my @constraints = ( { mid => 20, sd => 15 }, { mid => 30, sd => 25 }, { mid => 50, sd => 10 }, ); my $possibles = genPossibles( \@constraints ); printf "There are %d possible selections\n", scalar @$possibles; print "Here is one of them: @{ @{ $possibles }[ rand @$possibles ] }"; __END__ C:\test>junk7 There are 651 possible selections Here is one of them: 20 39 41 C:\test>junk7 There are 651 possible selections Here is one of them: 27 32 41 C:\test>junk7 There are 651 possible selections Here is one of them: 17 29 54