in reply to Re: Re: Re: Weighting rand()
in thread Weighting rand()

If your intention is to save memory, you can avoid the duplication, at the cost of more run time:
my %mirror = ( 'http://foo.mirror.org' => 3, 'http://www.users-domain.com/proj/foo' => 2, 'http://osdn.dl.sf.net/sourceforge/foo' => 3, 'http://unc.dl.sf.net/sourceforge/foo' => 4, 'http://umn.dl.sf.net/sourceforge/foo' => 4, 'http://heanet.dl.sf.net/sourceforge/foo' => 5, 'http://aleron.dl.sf.net/sourceforge/foo' => 5, ); my $t = 0; my $choice; while (my ($url, $n) = each %mirror) { $choice = $url if rand ($t += $n) < $n }
Of course, in real code, this is only worthwhile if your numbers are high, and the number of mirrors isn't.

Abigail