Comment detail

2次元ランダムウォーク (Nested Flatten)

This comment is reply for 6870 186: なぜかRで 1-1と2-1は解いてませ...(2次元ランダムウォーク). Go to thread root.

2次元も1次元と同じような書き方でいいと思いますが。
1
2
w <- floor(4*runif(t)+1)
plot(cumsum(c(1,-1,0,0)[w]),cumsum(c(0,0,1,-1)[w]),type="b")

もっといえば、1~4の乱数が欲しいようなときはrunif()ではなくsample()を使った方がRっぽいかもです。

1
plot(cumsum(data.frame(x=c(1,-1,0,0), y=c(0,0,1,-1))[sample(4, t, replace=T),]), type="b")

Index

Feed

Other

Link

Pathtraq

loading...