Julia 中一维随机游走的直方图

问题描述

这是对 100 个一维随机“步行者”的模拟,每个“步行者”在一个方向或另一个方向上以 +1/-1 的速度走 100 个“步”。

using Plots
init = 0
walkers = 100
walk_length = 100
walks = cumsum(vcat(fill(init,1,walkers),# initial state
                    rand([-1,1],walk_length,walkers)),# vertically append move direction
               dims=1)  # cumulative sum over the first dimension to get one walk per column
plot(walks,legend=nothing)

enter image description here

由于许多步行者可以在 100 步时达到相同的值,我想在终点创建一个直方图,显示那里的步行者数量
我认为可能有一个 hist() 函数,但我不确定如何实现它。

解决方法

histogram(walks[end,:],bins=20,legend=nothing)

enter image description here