したがって、私の現在のコードは「ランダムウォーク」問題を効果的に実行し、ピタゴラスの定理を使用して実際の距離を歩いた単位で計算しますが、プログラムを修正して、その歩行を一定回数試行してから計算できるようにする必要があります。平均二乗距離。答えだけを探しているのではなく、学習して再現できるように説明も必要です。別のwhileループが必要だと思いますが、どこに置くべきかわかりません。
import javax.swing.JOptionPane;
String a = JOptionPane.showInputDialog("Enter # of footsteps.");
int z = Integer.valueOf(a);
int x= 0; // starting x position
int y= 0; // starting y position
double r;
int counterZ = 0;
if (z < counterZ ){
System.out.println("Error");
}
while ( z > counterZ){
r=Math.random();
if (r<0.25){
x=x+1;
}
else if(r > .25 && r<0.50){
x=x-1;
}
else if(r > .5 && r<0.75){
y=y+1;
}
else{
y=y-1;
}
counterZ = counterZ + 1;
System.out.println("(" + x + "," + y + ")");
}
System.out.println("distance = " + round(sqrt((x*x)+(y*y))));