• 醉汉游走计算打车的概率


    改进后的代码实现:

    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
  • 相关阅读:
    【一篇文章就够了】Apollo
    【一篇文章就够了】Redis入门
    使用IDEA学习廖雪峰官方Git教程
    SourceTree的使用介绍
    【黑马旅游网】总结
    Entity Framework数据迁移命令使用总结
    leetcode editor
    IDEA个性化配置
    mysql5.7驱动(tableau,excel)
    正则表达式(1)
  • 原文地址:https://www.cnblogs.com/jpr-ok/p/12458447.html
Copyright © 2020-2023  润新知