Last active
December 24, 2015 06:59
-
-
Save ericzhang-cn/6761222 to your computer and use it in GitHub Desktop.
Revisions
-
ericzhang-cn renamed this gist
Sep 30, 2013 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
ericzhang-cn revised this gist
Sep 30, 2013 . No changes.There are no files selected for viewing
-
ericzhang-cn created this gist
Sep 30, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,30 @@ # -*- 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) #成功频率,以此作为概率估算值,结果保留两位小数