Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

python - Penney's Game probabilities

I want to repeat a list ("llista") several times to see how many times does one outcome win (XXX) or the other one (CCC) to calculate their probabilities. This list has to generate randomly again every time I want to know who won the game. I would be grateful if someone helped me finish my college homework.

CCC=0
XXX=0
llista=[]
i=0
llan?aments=100
a=1

while i < llan?aments:
    llista.append(np.random.randint(0,2))
    i+=1

for i in range (len(llista)):
    if i+2 >= len(llista):
        break
    if llista[i] == 0 and llista[i+1] == 0 and llista[i-1] == 0:
        CCC+=1
        break
    if llista[i] == 1 and llista[i+1] == 1 and llista[i-1] == 1:
        XXX+=1
        break

print(CCC)
print(XXX)
question from:https://stackoverflow.com/questions/66055351/penneys-game-probabilities

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This is how I would go about it.

import random

CCC=0
XXX=0

for count in range (100) :
    llista = [random.randint (0, 2) for x in range (3)]
    if llista == [0, 0, 0] :
        CCC += 1
    if llista == [1, 1, 1]:
        XXX += 1

print (f'CCC equals {CCC}.')
print (f'XXX equals {XXX}.')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...