首页 > 解决方案 > 获取关于从固定起点 (0,0) 的最大距离分布的信息

问题描述

我创建了一个模拟随机游走的程序,并将最大距离存储在一个列表中。步数是随机生成的。我正在尝试生成有关距固定起点的最大距离分布的数据(中值、均值、方差等)。但是,我相信我这样做是非常错误的。

代码:

walk_seed = 2025678

num_steps = 500 + 10 * (walk_seed % 11) - 50

np.random.seed(walk_seed)

journeys = 20000

max_distance = np.zeros(journeys)

for j in range(journeys):
    x_c = 0
    y_c = 0
    max_x = 9
    max_y = 6
    current_max_distance = math.sqrt(max_x**2+max_y**2)
    
    for i in range(num_steps):
        
        direction = np.random.randint(0,3)
        
        if direction == 0:
            x_c += 1
        if direction == 1:
            x_c -= 1
        if direction == 2:
            y_c += 1
        if direction == 3:
            y_c -= 1
            
        current_distance = math.sqrt(x_c * x_c + y_c * y_c)
        current_distance_round = math.trunc(current_distance)
        # print(current_distance_round)
        
        if current_distance_round > current_max_distance:
            current_max_distance = current_distance_round 
            
            
    max_distance[j] = current_distance_round 

总共有 20000 次步行,所有步行都有固定的步数和最大距离。目前,如果当前距离大于最大距离,它将存储在列表中。但是,最大距离似乎无关紧要,因为我得到的数字太大(平均值 = 160,方差 = 107)。

标签: pythonnumpystatisticssimulation

解决方案


推荐阅读