You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

石头剪刀布连续10次平局概率的10000次模拟平均结果计算技术问询

Fixing Your Rock-Paper-Scissors Consecutive Ties Simulation

Let's break down the issues in your original code and fix it to correctly run 10000 simulations and output the average result you're looking for.

Key Issues in Your Original Code

  • Undefined variables: You use roll before initializing it, which will throw a NameError.
  • Tangled loops: The nesting of your simulation loop and count loop is incorrect, leading to unexpected termination or infinite loops.
  • Incorrect tracking: Your total variable is adding roll (which is always 10 when you hit that line) instead of the actual number of rounds needed to reach 10 consecutive ties.
  • Inefficient imports: import random is placed inside an inner loop—this should be at the top of your file.
  • Misaligned counting: The tries variable doesn't account for all rounds played (each tie or non-tie is a valid round that should be counted).

Corrected Code: Average Rounds to Get 10 Consecutive Ties

This version simulates how many total rounds are needed to get 10 consecutive ties, runs this 10000 times, and outputs the average. The result should approach the theoretical expected value of (3^11 - 3)/2 = 88572.

import random

def simulate_consecutive_ties(target_ties=10):
    """Simulate one run to count rounds needed to reach target consecutive ties"""
    consecutive_ties = 0
    total_rounds = 0
    
    while consecutive_ties < target_ties:
        # Generate random choices (1=rock, 2=paper, 3=scissors)
        p1 = random.randint(1, 3)
        p2 = random.randint(1, 3)
        total_rounds += 1
        
        if p1 == p2:
            consecutive_ties += 1
        else:
            # Reset streak if no tie
            consecutive_ties = 0
    
    return total_rounds

def main():
    num_simulations = 10000
    total_rounds_all = 0
    
    for _ in range(num_simulations):
        total_rounds_all += simulate_consecutive_ties()
    
    average_rounds = total_rounds_all / num_simulations
    theoretical_expected = (3**11 - 3) / 2
    
    print(f"Completed {num_simulations} simulations")
    print(f"Average rounds needed for 10 consecutive ties: {average_rounds:.2f}")
    print(f"Theoretical expected value: {theoretical_expected}")

if __name__ == "__main__":
    main()

Alternative: Empirical Probability of 10 Consecutive Ties in 10 Rounds

If your goal was to calculate the empirical probability of getting 10 ties in a single set of 10 rounds (which should approach (1/3)^10 ≈ 0.0000169), use this code instead:

import random

def has_10_consecutive_ties():
    """Check if 10 consecutive rounds are all ties"""
    for _ in range(10):
        p1 = random.randint(1, 3)
        p2 = random.randint(1, 3)
        if p1 != p2:
            return False
    return True

def main():
    num_simulations = 10000
    success_count = 0
    
    for _ in range(num_simulations):
        if has_10_consecutive_ties():
            success_count += 1
    
    empirical_prob = success_count / num_simulations
    theoretical_prob = (1/3)**10
    
    print(f"Completed {num_simulations} simulations")
    print(f"Empirical probability of 10 consecutive ties: {empirical_prob:.8f}")
    print(f"Theoretical probability: {theoretical_prob:.8f}")

if __name__ == "__main__":
    main()

内容的提问来源于stack exchange,提问作者Joe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 07:01:03