my @thresh; my $total = 0; for ( keys %hash ) { $total += $hash{$_}; push @thresh, [ $total, $_ ]; } my $string; for ( 1 .. 30 ) { my $r = rand $total; my $i = 0; while ( $r > $thresh[$i][0] ) { $i++; } $string .= $thresh[$i][1]; }