#!/usr/bin/perl -wd use strict; my $tot = 0; my $i = 0; my $x = 0; my @array; while( $tot < 100 ) { my $x = int rand( 100 ); $x = ( $tot + $x > 100 ) ? 100 - $tot : $x; $array[$i] = $x; $i++; $tot += $x; } $tot = 0; foreach( @array ) { print $_, "\n"; $tot += $_; } print "Total is ", $tot, "\n";