note ferrency <b>Update:</b>I'm totally wrong! Please ignore me! And take my advice: try the code before you claim it's broken :) Sorry, IO.<p> I don't think you want to put the <code>rand()</code> inside the loop. I think to get the correct results, you need to choose one random number outside the loop, and test it vs. the sum as you go along.<p> Try out a simple case by hand:<p> <code> my %bias = (1 => 1, 2 => 1); </code> Assuming your first iteration picks up (1 => 1), you have: <code> \$rand = \$k if rand(\$sum += \$v) <= \$v; # This simplifies to: # \$rand = 1 if rand(1) <= 1 # this is always true. That's wrong. </code> What you really want is something like this: <code> my \$sum = 0; \$sum += \$_ foreach (values %bias); my \$target = rand(\$sum); \$sum = 0; while (my (\$k, \$v) = each %bias) { if (\$target <= (\$sum += \$v)) { \$rand = \$k; last; } } </code> I'm sure there's a golfier way to do it, but this demonstrates the basic idea.<p> <b>Update:</b> Okay, after actually trying the code, I believe I'm totally wrong. I think that what I thought was a combination of two bugs may actually be a clever solution. Though I'm still not sure I believe it produces the correct result distribution. IO, would you care to describe how it works? Sorry about that.<p> Alan<p> 158482 158490