'Trouble starting Random Walker java program at 0,0 coordinates
I am trying to build a program that takes an integer and simulates the motion of a random walk until the random walker is at Manhattan distance r from the starting point. One of the key requirements is to start the program at 0,0 coordinates but I am unable
public class RandomWalker {
public static void main(String args[]) {
int r = Integer.parseInt(args[0]);
int x = 0;
int y = 0;
int step = 0;
double R;
while(Math.abs(x) < r && Math.abs(y) < r) {
step++;
R = Math.random();
if(R <= 0.25) x++;
else if (R <= 0.50) x--;
else if (R <= 0.75) y++;
else if (R <= 1.00) y--;
System.out.println("(" + x + ", " + y + ")" );
}
System.out.println("steps = " + step);
}
}
Solution 1:[1]
I suspect you just need to print the coordinates before stepping. See below. I've also tried to encapsulate the walker values in the class rather than the main method.
public class RandomWalker {
private final Random random;
private int steps = 0;
private int x = 0;
private int y = 0;
public RandomWalker(Random random) {
this.random = random;
}
public String toString() {
return steps + ": (" + x + "," + y + ")";
}
public void step() {
steps++;
switch (random.nextInt(4)) {
case 0: x++; break;
case 1: x--; break;
case 2: y++; break;
case 3: y--; break;
}
}
public int distance() {
return Math.abs(x) + Math.abs(y);
}
public static void main(String args[]) {
int r = Integer.parseInt(args[0]);
RandomWalker walker = new RandomWalker(new Random());
while (walker.distance() < r) {
System.out.println(walker);
walker.step();
}
System.out.println("steps = " + walker.steps);
}
}
Solution 2:[2]
I think you print the (x,y) first, before the circle. Then you will get an (0,0) and the condition should be <r, iso <=r, otherwise the steps will count 1+ time.
Solution 3:[3]
The solution above is pretty good but this is an exam from the WEEK2 Princeton course
Computer Science: Programming with a Purpose and they only use Java features that have already been introduced in the course (e.g., loops and conditionals, but not arrays or functions).
Here is a solution that is acceptable:
public class RandomWalker {
public static void main(String[] args) {
int r = Integer.parseInt(args[0]);
int x = 0;
int y = 0;
int distance = 0;
int steps = 0;
System.out.println("(" + x + "," + y + ")");
while (distance < r) {
double i = Math.random();
if (i < 0.25) {
y = y + 1;
System.out.println("(" + x + "," + y + ")");
distance = Math.abs(x) + Math.abs(y);
steps += 1;
}
if (i < 0.5) {
x = x + 1;
System.out.println("(" + x + "," + y + ")");
distance = Math.abs(x) + Math.abs(y);
steps += 1;
}
if (i < 0.75) {
y = y - 1;
System.out.println("(" + x + "," + y + ")");
distance = Math.abs(x) + Math.abs(y);
steps += 1;
}
if (i >= 0.75 && i < 1) {
x = x - 1;
System.out.println("(" + x + "," + y + ")");
distance = Math.abs(x) + Math.abs(y);
steps += 1;
}
}
System.out.println("Steps = " + steps);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | sprinter |
| Solution 2 | F. Müller |
| Solution 3 | mplanic |
