0

如何生成具有 n 维的 RandomWalk?

我已经有一些 1 维或 2 维随机游走的例子,但现在我必须编写一个 n 维的随机游走,我真的不知道怎么做。

以下是 2 个维度的 2 个示例:

 RandomWalk2DLattice[n_] := 
 Accumulate[Through[{Cos, Sin}[# \[Pi]/2]] & /@ RandomInteger[3, {n}]]
 rw = RandomWalk2DLattice[500];

 Show[Graphics[{Line[rw], {PointSize[.02], Point[rw[[{-1}]]], 
        Point[{0, 0}]}}, Axes -> True], AspectRatio -> Automatic]

有人有答案吗?

4

1 回答 1

1
ndim = 3;
walk = NestList[(d = RandomInteger[{1, ndim}]; 
         ReplacePart[#, d -> #[[d]] + RandomChoice[{-1, 1}]]) &,
                ConstantArray[0, ndim], 1000];

在此处输入图像描述

于 2016-05-06T20:18:46.083 回答