times=10000
true1=0
true2=0
while times!=0 :
times-=1
car=random.randint(1,3)
choose1=random.randint(1,3)
teller=car+1
if teller>3:
teller=1
if teller==choose1:
teller+=1
if car==choose1:
true1+=1
continue
if car!=teller:
true2+=1
print true1
print " "
print true2
print true1
print " "
print true2
优化后,显得很简单了,第一次选对的概率是1/3
改选的概率是1-1/3=2/3
就是第一次不对,第二次一定对
import random
times=10000
true1=0
true2=0
now=times
while now!=0 :
now-=1
car=random.randint(1,3)
choose1=random.randint(1,3)
if car==choose1:
true1+=1
很好理解 python写的算
很好理解
python写的算法
import random
times=10000
true1=0
true2=0
while times!=0 :
times-=1
car=random.randint(1,3)
choose1=random.randint(1,3)
teller=car+1
if teller>3:
teller=1
if teller==choose1:
teller+=1
if car==choose1:
true1+=1
continue
if car!=teller:
true2+=1
print true1
print " "
print true2
print true1
print " "
print true2
优化后,显得很简单了,第一次选对的概率是1/3
改选的概率是1-1/3=2/3
就是第一次不对,第二次一定对
import random
times=10000
true1=0
true2=0
now=times
while now!=0 :
now-=1
car=random.randint(1,3)
choose1=random.randint(1,3)
if car==choose1:
true1+=1
true2=times-true1
print true1
print " "
print true2