Talk:Unbias a random generator

From Rosetta Code

Probabilities are weird!

After completing my unbiasing function: <lang python>def unbiased(biased):

   'uses a biased() generator of 1 or 0, to create an unbiased one'
   this, that = biased(), biased()
   while this == that: # Loop until 10 or 01
       this, that = biased(), biased()
   return this         # return the first</lang>

I took a look and thought about those two calls to biased() in the inner loop. "It should work with just one call" I thought. (All the while thinking that this is probability and it always bites me).

I could not resist. I reasoned that all I was looking for is a 1,0 or 0,1 in the stream generated by biased() it should not matter if I look at non-overlapping pairs, as above, or just slide along a two bit window into the stream like this: <lang python>def unbiased(biased):

   'uses a biased() generator of 1 or 0, to create an unbiased one'
   this, that = biased(), biased()
   while this == that: # Loop until 10 or 01
       this, that = that, biased()  #<<<
   return this         # return the first</lang>

Sure enough the change fails. It is just as biased as the biased generator.

I guess I'm doomed to never intuit probabilities; but at least I remember that most times now :-)
--Paddy3118 07:19, 22 February 2011 (UTC)

Just in case you have not worked this out yet: the issue here is that 0 0 is much more likely than 1 1, so when you iterate, your new value of 'this' will almost always be zero. --Rdm 16:18, 22 February 2011 (UTC)
I was leaning towards that explanation. Thanks for the confirmation. --Paddy3118 16:51, 22 February 2011 (UTC)