醉汉游走计算打车的概率
2020-03-10 20:40 改改~_~ 阅读(188) 评论(0) 编辑 收藏 举报改进后的代码实现:
import random def random_walk_v2(n): x,y=0,0 for i in range(n): (dx,dy)=random.choice([(0,1),(0,-1),(-1,0),(1,0)]) #四个方向选一个 x+=dx y+=dy return (x,y) for i in range(100): one_walk=random_walk_v2(20) print(one_walk,'How far away from (0,0):',abs(one_walk[0])+ abs(one_walk[1])) taking_a_taxi_counter=0 how_money_simulations=100 how_steps=200 legs_limits=100 for i in range(how_money_simulations): one_walk=random_walk_v2(how_steps) how_far=int(one_walk[0]**2+int(one_walk[1]**2)) if how_far>=legs_limits: # print(one_walk,'It is too far away from home ~I am taking texi') taking_a_taxi_counter+=1 else: pass # print(one_walk ,'I can walk home') # print(one_walk ,how_far) print('% of taking a taxi home:', taking_a_taxi_counter/how_money_simulations)
每次运行都会出来一个概率:
(-1, -3) How far away from (0,0): 4 (-2, 0) How far away from (0,0): 2 (-3, 3) How far away from (0,0): 6 (2, -2) How far away from (0,0): 4 (-2, 6) How far away from (0,0): 8 (-4, 0) How far away from (0,0): 4 (-3, 1) How far away from (0,0): 4 (0, 0) How far away from (0,0): 0 (1, -1) How far away from (0,0): 2 (-1, -1) How far away from (0,0): 2 (1, -3) How far away from (0,0): 4 (2, -2) How far away from (0,0): 4 (5, -3) How far away from (0,0): 8 (2, 4) How far away from (0,0): 6 (-4, 2) How far away from (0,0): 6 (4, -4) How far away from (0,0): 8 (1, -3) How far away from (0,0): 4 (1, -3) How far away from (0,0): 4 (0, 2) How far away from (0,0): 2 (-6, -2) How far away from (0,0): 8 (-2, -2) How far away from (0,0): 4 (1, 5) How far away from (0,0): 6 (4, 4) How far away from (0,0): 8 (3, -1) How far away from (0,0): 4 (-5, -1) How far away from (0,0): 6 (-4, 0) How far away from (0,0): 4 (-1, -5) How far away from (0,0): 6 (1, 3) How far away from (0,0): 4 (-4, 0) How far away from (0,0): 4 (-2, 0) How far away from (0,0): 2 (-2, -4) How far away from (0,0): 6 (-4, 0) How far away from (0,0): 4 (-1, -3) How far away from (0,0): 4 (2, 4) How far away from (0,0): 6 (-2, 2) How far away from (0,0): 4 (2, -4) How far away from (0,0): 6 (-1, -1) How far away from (0,0): 2 (-3, 3) How far away from (0,0): 6 (1, 1) How far away from (0,0): 2 (0, 6) How far away from (0,0): 6 (-1, -3) How far away from (0,0): 4 (-4, -2) How far away from (0,0): 6 (1, -1) How far away from (0,0): 2 (0, -4) How far away from (0,0): 4 (-3, 3) How far away from (0,0): 6 (4, 0) How far away from (0,0): 4 (1, 3) How far away from (0,0): 4 (2, 6) How far away from (0,0): 8 (5, -1) How far away from (0,0): 6 (-4, -4) How far away from (0,0): 8 (2, 2) How far away from (0,0): 4 (-2, 0) How far away from (0,0): 2 (-4, -4) How far away from (0,0): 8 (-2, -4) How far away from (0,0): 6 (3, -1) How far away from (0,0): 4 (0, -2) How far away from (0,0): 2 (1, 5) How far away from (0,0): 6 (3, -1) How far away from (0,0): 4 (-3, -3) How far away from (0,0): 6 (1, 1) How far away from (0,0): 2 (1, -1) How far away from (0,0): 2 (-2, 4) How far away from (0,0): 6 (-2, -2) How far away from (0,0): 4 (-5, 1) How far away from (0,0): 6 (-2, 4) How far away from (0,0): 6 (1, -3) How far away from (0,0): 4 (2, -4) How far away from (0,0): 6 (1, 1) How far away from (0,0): 2 (0, 6) How far away from (0,0): 6 (3, 1) How far away from (0,0): 4 (-3, 1) How far away from (0,0): 4 (-2, 0) How far away from (0,0): 2 (-2, 0) How far away from (0,0): 2 (0, -2) How far away from (0,0): 2 (3, 5) How far away from (0,0): 8 (0, -2) How far away from (0,0): 2 (3, 1) How far away from (0,0): 4 (-5, 1) How far away from (0,0): 6 (3, 5) How far away from (0,0): 8 (-3, -5) How far away from (0,0): 8 (3, 1) How far away from (0,0): 4 (1, -1) How far away from (0,0): 2 (-2, 2) How far away from (0,0): 4 (-2, 0) How far away from (0,0): 2 (-2, -4) How far away from (0,0): 6 (3, 3) How far away from (0,0): 6 (-4, 2) How far away from (0,0): 6 (-3, 9) How far away from (0,0): 12 (-2, 6) How far away from (0,0): 8 (2, 2) How far away from (0,0): 4 (-1, 1) How far away from (0,0): 2 (3, -1) How far away from (0,0): 4 (1, 3) How far away from (0,0): 4 (0, 0) How far away from (0,0): 0 (5, -3) How far away from (0,0): 8 (-4, 4) How far away from (0,0): 8 (5, 5) How far away from (0,0): 10 (-1, -1) How far away from (0,0): 2 (-5, -3) How far away from (0,0): 8 (3, 1) How far away from (0,0): 4 % of taking a taxi home: 0.59