Recently I stumbled upon a problem on stackoverflow.com which roughly translates to this:
You have a function F1 which returns True with probability P. Build another function F2 that uses F to return True with probability 1/2.
The solution is rather nice and clean :
F2():I leave it for a further edit of this post to include the generalization of F2 for returning True with probability 1/N.
result1 = F1()
result2 = F1()
if result1 = True and result2 = False: return True
elif result1 = False and result2 = True: return False