#!/usr/local/bin/perl use strict; use warnings; sub make_random_picker { my ( $t, @choices ); for my $choice ( @{ shift @_ } ) { push @choices, ( $choice->[0] ) x $choice->[1]; $t += $choice->[1]; } return sub { $choices[ int( rand() * $t ) ] }; } my $pick = make_random_picker([ [ foo => 20 ], [ bar => 10 ], [ baz => 10 ], [ hoge => 30 ], [ moge => 30 ], ]); print $pick->(), "\n" for (1..20);