<div class="gmail_quote">On 11 June 2012 13:56, James Coglan <span dir="ltr"><<a href="mailto:jcoglan@gmail.com" target="_blank">jcoglan@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>I have a problem I'm not sure how to solve, more of a general programming problem than a Ruby thing. Basically, say you have an indefinite stream of random bits:</div><div><br></div><div><div>
1100111001110110101100000110101011011110110011111001101000001000111000100000011001111110100010000001000001111110100010010100111111011101110110011111001101010101101111111011101111111000010001001001100100100101011100001001000001110101011101101101010011100111 ...</div>
<div><br></div><div>and you want to turn it into a stream of characters from an arbitrary-size list of characters, let's say the 26 letters from A to Z. You want the letters to be evenly distributed in the output; each letter should have an equal probability of appearing.</div>
</div></blockquote><div><br></div><div>As an illustration, my current approach is to pop as many bits as I can off the front of the stream such that they give a number up to the size of the set. This produces a bias where some characters are 3x as likely to appear as others:</div>
<div><br></div><div>{ '0': 3102,</div><div> '1': 3160,</div><div> '2': 3094,</div><div> '3': 3070,</div><div> '4': 3167,</div><div> '5': 3185,</div><div> '6': 3118,</div>
<div> '7': 3197,</div><div> '8': 3230,</div><div> '9': 3096,</div><div> '10': 3109,</div><div> '11': 3072,</div><div> '12': 3087,</div><div> '13': 9375,</div>
<div> '14': 9362,</div><div> '15': 9291,</div><div> '16': 3137,</div><div> '17': 3166,</div><div> '18': 3134,</div><div> '19': 3190,</div><div> '20': 3102,</div>
<div> '21': 3060,</div><div> '22': 3109,</div><div> '23': 3176,</div><div> '24': 3049,</div><div> '25': 3162 } </div></div>