Simple 2D random move

I have a problem with the average distance in this exercise. It should be close to sqrt of N steps, but it is lower. Can you help me find out where my mistake is?

2D random walk. Two-dimensional random walk models the behavior of a particle moving in a grid of points. At each step, a random walker moves north, south, east or west with a probability of 1/4, regardless of previous moves. Determine how far (on average) the random walker is from the starting point after N steps. (Theoretical answer: in order 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"); } } 
+5
source share
2 answers

I did some tests on your code with the aforementioned range change <0,1), <1,2), <2,3), <3,4), which makes them even.

And you do it like this:

 if (nextStep < 1) ++moveWest; else if (nextStep < 2) ++moveEast; else if (nextStep < 3) ++moveSouth; else if (nextStep < 4)++moveNorth; 

Pay attention to <= , becoming < .

100,000 tests in 100 steps each gave these results:

 Average distance is 8.873435509749317 steps away of from the starting point W=2498906 E=2501447 N=2500022 S=2499625 

, where W, E, N, S are the summarized steps for a given direction during all tests. They look great.

Running such a test case a couple of times shows that there is no preferred direction. You can use other methods to get random numbers, but this will be testing the generators, not your business. Your code looks fine from my point of view.

The sentence from the problem proposal also gives you the key: Theoretical answer: in the order sqrt (N).

+3
source

I think this line will not work:

 nextStep = Math.random()*4; 

the explanation is logical. I think it would be better to use Goals for your purpose, because you want to calculate using steps that are static. Well, this opinion is based, but I recommend counting the total number of steps instead of tracking partial steps.

try this instead:

 Random rand = new Random(); nextStep = rand.nextInt(4)+1; //random numbers {1,2,3,4} 

Also, since nextInt() generates random Integer values, you need to use the == operator instead of <= in your if / else statements.

 if (nextStep == 1) ++moveWest; else if (nextStep == 2) ++moveEast; else if (nextStep == 3) ++moveSouth; else if (nextStep == 4)++moveNorth; 

regarding Tom (not me, the one in the comments!)

+1
source

Source: https://habr.com/ru/post/1232623/


All Articles