Probability

This solution for generating a probability between 0 and 1 using a fair coin is an application of binary fractional representation and the convergence properties of infinite series. Let's break it down:

1. **Binary Fraction Representation**:

The idea is to use the sequence of coin flips to construct a binary fraction. In binary, the fraction 0.1 (in decimal) is equivalent to \( \frac{1}{2} \), 0.01 is \( \frac{1}{4} \), and so on. So, every coin flip adds another digit to the binary fraction, where heads (1) adds a higher value than tails (0).

2. **Generating the Random Number \( s \)**: The sum \( \sum_{n=1}^{\infty} \frac{s_n}{2^n} \) is an infinite series where each \( s_n \) is either 0 or 1 (based on the coin flip), and \( \frac{1}{2^n} \) is the binary fraction value of the nth flip. As you keep flipping the coin, you add more digits to this binary fraction, getting closer and closer to a specific real number between 0 and 1.

3. **Stopping Criterion**: The clever part of this method is knowing when to stop flipping the coin. If at any point the sum of the series so far (\( s_i \)) is such that no matter what the future flips are, it can't reach \( p \) (your target probability), you stop. There are two cases:

- If \( s_i < p_i \), then even if all future flips are heads (1s), \( s \) will never reach \( p \). So, you declare a loss.

- If \( s_i > p_i \), then even if all future flips are tails (0s), \( s \) will still be greater than \( p \). So, you declare a win.

import random

def flip_coin():
    return random.choice([0, 1])

def generate_probability(p):
    s = 0
    i = 1

    while True:
        flip = flip_coin()
        s += flip / (2 ** i)
        
        if s < p / (2 ** i):
            return "Loss", s
        elif s > p / (2 ** i):
            return "Win", s

        i += 1

# Example usage
p = 0.75  # Target probability
result, final_s = generate_probability(p)
print(f"Result: {result}, Final s: {final_s}")

Last updated