What am I doing wrong with this RandomWalker code?


#1

Write a program RandomWalkers.java that takes two integer 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).

public class RandomWalkers {
public static void main(String[ ] args) {
int n = Integer .parseInt(args[0]);
int t = Integer.parseInt(args[1]);
int x = 0;
int y = 0;
int i = x * x + y * y;
while (t > 0)
{
t = t - 1;
while (n > 0)
{
double a = Math.random();
n = n - 1;
if (a <= 0.25) {
x = x + 1;
} else if (a <= 0.5) {
x = x - 1;
} else if (a <= 0.75) {
y = y + 1;
} else if (a <= 1.00) {
y = y - 1;
}
}
i = i + x * x + y * y;
}
int v = Integer.parseInt(args[1]);
System.out.println("mean squared distance = " + i / v);
}
}


#2

First of all this is the javaScript category not the Java one. Also here the 3. line is a synonym for 0:

int x = 0;
int y = 0;
int i = x * x + y * y

and be careful with integer division in java:
System.out.println("mean squared distance = " + i / v);

1/2 = 0 aso. For more please post in the right category with more information given.