In the newsgroup comp.arch Achim Gratz asked:

This is actually quite an interesting problem, if one is concerned with performance. First, lets point out that a sufficient answer can be made quite easily:

Which just requires a good random number generator. Now as to the matter of performance, I proposed an approach where a typical word based random number generator is used. And I noted the following:

Basically, given the set P of all possible densities that can be constructed, then for any x, y in P, we have that (x*y), (x+yx*y) and (x+y2*x*y) (corresponding to &, or, and exclusive or) are also in the set P. P also contains 0.5 (just calling RandWord()). Using this approach, one can approximate any given density with low accuracy fairly quickly.
In the newsgroup comp.programming Andrew asked:

I came up with two solutions. Here is the first:

which was improved by Michael Mendelsohn as

I stumbled upon the second idea when considering other alternative distribution modification functions:
