You're reading: cp's mathem-o-blog

Converting a stream of binary digits to a stream of base $n$ digits

James Coglan asked on twitter:

Suggestions wanted: how to turn an indefinite stream of bits (01110010…) into a stream of [A-Z] where letters are evenly distributed.

— James Coglan (@jcoglan) June 11, 2012

And I don’t mean just select character codes, I mean select from an arbitrary-sized character set of any length.

— James Coglan (@jcoglan) June 11, 2012

 

So you have an infinite stream of uniform random binary digits, and want to use it to produce an infinite stream of uniform random base $n$ digits.

The obvious really easy way to do it is to find the smallest $k$ such that $2^k \geq n$, and generate numbers in the range $0 \dots 2^k-1$.

Continue reading “Converting a stream of binary digits to a stream of base $n$ digits” on cp’s mathem-o-blog