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$/

In reply to Re: random elements with fixed totals by jonadab
in thread random elements with fixed totals by ysth

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.