HTHTorTHTH

an anonymous user · December 03, 2023
print('Hello world!')
import matplotlib.pyplot as plt
import random

def simulate_coin_tosses(n_flips):
    return ''.join(random.choice('HT') for _ in range(n_flips))

def simulate_betting(sequence):
    total_balance = 0
    balance_curve = []

    bet_on_T = False
    bet_on_H = False

    for i in range(len(sequence)):
        if sequence[i:i+4] == 'HTHT' and not bet_on_T:
            total_balance += 10
            bet_on_T = True
        elif sequence[i:i+4] == 'THTH' and not bet_on_H:
            total_balance += 10
            bet_on_H = True

        if bet_on_T and sequence[i:i+4] == 'HTHT':
            total_balance += 10
            bet_on_T = False
        elif bet_on_H and sequence[i:i+4] == 'THTH':
            total_balance += 10
            bet_on_H = False

        balance_curve.append(total_balance)

    return balance_curve

# Simulate 500 coin flips
sequence = simulate_coin_tosses(500)

# Simulate betting based on the sequence
balance_curve = simulate_betting(sequence)

# Plot the total balance curve
plt.plot(balance_curve)
plt.xlabel('Flip Number')
plt.ylabel('Total Balance')
plt.title('Total Balance Curve with Betting Strategy')
plt.show()
Output

Comments

Please sign up or log in to contribute to the discussion.