You are given a black-box function, notrand(), which returns 0 with 60% probability and 1 with 40% probability. Create a new function, rand(), which returns 0 and 1 with equal probability (i.e. 50% each) using only notrand() as your source of randomness.
I've heard many interesting approaches to the problem. I'll post my own solution later after hearing some from the community!
Easy Math Editor
This discussion board is a place to discuss our Daily Challenges and the math and science related to those challenges. Explanations are more than just a solution — they should explain the steps and thinking strategies that you used to obtain the solution. Comments should further the discussion of math and science.
When posting on Brilliant:
*italics*
or_italics_
**bold**
or__bold__
paragraph 1
paragraph 2
[example link](https://brilliant.org)
> This is a quote
\(
...\)
or\[
...\]
to ensure proper formatting.2 \times 3
2^{34}
a_{i-1}
\frac{2}{3}
\sqrt{2}
\sum_{i=1}^3
\sin \theta
\boxed{123}
Comments
Here is a possible function for rand():
let a0, a1 use notrand() to assign a value. so a0 = {0: 60%, 1: 40%}, a1 = {0: 60%, 1: 40%}.
Consider a0 + a1:
0(60%) + 0(60%) = 0b00
0(60%) + 1(40%) = 0b01
1(40%) + 0(60%) = 0b01
1(40%) + 1(40%) = 0b10
Note the least significant bit (which can be found with the xor of a0 and a1) has a different weight than the notrand() function, namely:
a0 ⊕ a1 = {0: 52%, 1: 48%}
Now if we combine two of these results (4 calls to notrand() in total) the percentages change again to:
0(52%) ⊕ 0(52%) = 0
0(52%) ⊕ 1(48%) = 1
1(48%) ⊕ 0(52%) = 1
1(48%) ⊕ 1(48%) = 0
or {0: 50.08%, 1: 49.92%}
This pattern will approach 50/50 as the sub iterations get larger, however, the associative property of xor gives one more detail:
((a ⊕ b) ⊕ (c ⊕ d)) ⊕ ((i ⊕ j) ⊕ (k ⊕ l)) = (a ⊕ b ⊕ c ⊕ d ⊕ i ⊕ j ⊕ k ⊕ l)
and so xor'ing multiple calls of notrand() actually brings the function closer to 50/50 regardless of the number of calls used and the order they are combined in.