このエクササイズでは平均的な距離に問題があります。それはNステップのsqrtに近いはずですが、それは低くなっています。あなたは私の間違いがどこにあるのかを知る手伝いをすることができますか?シンプルな2Dランダムウォーク
2Dランダムウォーク。 2次元ランダムウォークは、点のグリッド内を移動するパーティクルの動作をシミュレートします。 ランダム歩行者は、確率1/4で北、南、東、または西に移動し、 は前の移動とは独立して移動します。ランダム歩行者がN歩後の出発点からどのくらい離れているか(平均して)決定する。 (理論的な答え:。SQRT(N)のオーダー)
public class RandomWalk{
public static void main(String[] args){
int N = Integer.parseInt(args[0]);
double nextStep = 0;
double averageDistance = 0;
int COUNT = 1000;
for (int j = 0; j < COUNT; j++){
int moveWest = 0;
int moveEast = 0;
int moveSouth = 0;
int moveNorth = 0;
double distance = 0;
for (int i = 0; i < N; i++){
nextStep = Math.random()*4;
if (nextStep <= 1) ++moveWest;
else if (nextStep <= 2) ++moveEast;
else if (nextStep <= 3) ++moveSouth;
else if (nextStep <= 4)++moveNorth;
}
moveEast = moveEast - moveWest;
moveNorth = moveNorth - moveSouth;
distance = Math.sqrt((moveEast * moveEast) + (moveNorth * moveNorth));
averageDistance += distance;
System.out.println("Walker is " + distance + "\t steps away of from the starting point");
//System.out.println("Sqrt of N is " + Math.sqrt(N));
}
System.out.println("Average distance is " + averageDistance/COUNT + " steps away of from the starting point");
}
}
レンジ<0,1>から> 3,4(より大きいので、私はそれをテストしたが、大幅な変更を行っていない、まだ...ここからスタート。でも、どこでも、比較未満を使用してそれらを作るで始まると思います。 – zubergu
私はそれがsqrt(N)やそれに類するものに収束するとは思わない。それはsqrt(N)の大きさのオーダーまたはそのようなものであると考えられる。またはmb O(sqrt(N))。数学... –
それから私の解が正しいと思いますか? –