Skip to content

Instantly share code, notes, and snippets.

@ericzhang-cn
Last active December 24, 2015 06:59
Show Gist options
  • Save ericzhang-cn/6761222 to your computer and use it in GitHub Desktop.
Save ericzhang-cn/6761222 to your computer and use it in GitHub Desktop.
Coursera几率课程,第五周第十题,模拟程序
# -*- coding: utf-8 -*-
from math import *
from random import *
### 一次测试 ###
def test():
speed = 4.0 #初速度
distance = 0 #已经走过的距离
threshold = log(50) / 100 #每秒中枪的概率
while distance < 400:
distance += speed #以当前速度移动
if random() < threshold: #如果中枪,则判断是否死亡,如果不死亡,速度减半
if speed == 0.5:
return 0 #死亡!
else:
speed /= 2
return 1 #成功通过!
### 模拟 ###
succ = 0.0 #成功次数
times = 100000 #模拟次数,可随需要更改,越大则越准确,但耗时越多
for i in range(0, times):
if test() == 1:
succ += 1
print round(succ / times, 2) #成功频率,以此作为概率估算值,结果保留两位小数
@petitchamp
Copy link

干脆建一个repo来放每一周的作业好了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment