in reply to random elements with fixed totals
If I just loop through, giving each element a chance to be set of (how many ones left to assign) over (how many elements are left), it seems to me that that will distort the distribution toward the end.
Yes, it may, but if so, it will distort the distribution in a random way. That is to say, one time it may run low on 1s early, distorting the later part of the distribution toward 0, and another time it may have extra 1s left for the later part of the distribution. This is a random distribution.
You need to understand the difference between a random distribution and an even distribution. If you write your algorithm to guarantee an even distribution, it's not random; if you write it to be random, you won't always get an even distribution. There's no way to guarantee both, because those are contradictory requirements.
If this troubles you, you can take your random distribution and randomly shuffle it, as the other monk suggested, but that still won't force it to be even.
If what you want is a mostly even distribution that's not fully predictable, you may want to take an even distribution and do localized shuffling (i.e., loop through once or twice and randomly swap some elements with other _nearby_ elements). The distribution you get from this will not *be* random, but it may appear random to the untrained eye, and it will be close to even.
$;=sub{$/};@;=map{my($a,$b)=($_,$;);$;=sub{$a.$b->()}} split//,".rekcah lreP rehtona tsuJ";$\=$ ;->();print$/
|
|---|