I had to write this for a class project. I don’t get any errors when compiling, but the program won’t run when I attempt to execute it.
We have to write a program myRandomWalkers.java that takes two command-line arguments N and T. In each of T independent experiments, simulate a random walk of N steps and compute the squared distance. Output the mean squared distance (the average of the T squared distances).
I wrote the following:
public class myRandomWalkers {
public static void main(String[] args) {
int n= Integer.parseInt(args[0]);
int t= Integer.parseInt(args[1]);
int x= 0; // starting x position
int y= 0; // starting y position
int sum = 0; // for calculating mean square distance
double r;
int count = 0;
while (count <= t)
{
for (int i=0; i<n; i++) {
r= Math.random();
if (r<=0.25) x++;
else if(r<=0.50) x--;
else if(r<=0.75) y++;
else if(r<=1.0) y--;
int z = ((x*x) + (y*y));
sum +=z;
}
}
int average = (sum/t);
System.out.println ("mean squared distance = " + average);
}
}
Thanks in advance!
The output will depend on the arguments you pass to your program. However:
count > tthe while loop will not be executedcount <= tthe while loop will never end because you don’t increment count – you probably need to add acount++;somewhere in your while loop.