擅长:python、mysql、java
<p><strong>旁白:</strong>您可以在Python中使用<code>_</code>来更容易地表示大数字。这可能很有趣,因为您是python新手</p>
<p>我想我们讨论的是连续六次击中头部或尾部的概率。如果是这样,您可以这样实现:</p>
<pre><code>streaks = 0
for attempt in range(10_000):
attempt_outcome = [random.randint(0, 1) for i in range(100)]
for i in range(len(attempt_outcome)-5):
if sum(attempt_outcome[i:i+5]) == 0 or sum(attempt_outcome[i:i+5]) == 6:
streaks +=1
break
print("Probability: {}".format(streaks / 10_000))
</code></pre>
<p>这使我们得出以下结论:</p>
<pre><code>>>> Probability: 0.8095
</code></pre>
<p>这意味着在10000次尝试中,有80.95%的尝试有一次或多次的连胜,超过六次</p>
<hr/>
<p><strong>注意:</strong>我们在发现第一条条纹后将打破for循环,因为在我们的情况下,如果尝试结果中有多条条纹,我们不需要这些信息。这会影响我们的计算并导致错误的结果</p>