0

I'm having some troubles trying to write a code for the following exercise:

Write a program RandomWalkers.java that takes two integer command-line arguments r and trials. In each of trials independent experiments, simulate a random walk until the random walker is at Manhattan distance r from the starting point. Print the average number of steps. An image preview here

The running of the program should manifest like this:

~/Desktop/loops> java RandomWalkers 5 1000000
average number of steps = 14.98188

~/Desktop/loops> java RandomWalkers 5 1000000
average number of steps = 14.93918

~/Desktop/loops> java RandomWalkers 10 100000
average number of steps = 59.37386

~/Desktop/loops> java RandomWalkers 20 100000
average number of steps = 235.6288

~/Desktop/loops> java RandomWalkers 40 100000
average number of steps = 949.14712

~/Desktop/loops> java RandomWalkers 80 100000
average number of steps = 3775.7152

~/Desktop/loops> java RandomWalkers 160 100000
average number of steps = 15113.61108

I tried to implement the code, but I am quite skeptical about my code logic.

public class RandomWalkers {
public static void main(String [] args){
    int r = Integer.parseInt(args[0]);
    int trials = Integer.parseInt(args[1]);
    int x = 0;
    int y = 0;
    int distance = 0;
    int sumOfSteps = 0;
    double averageSteps = 0;
    int steps = 0;
    for(int i = 1 ; i <=trials ; i++){
        while(distance!=r){
            double random = Math.random();
            if(random > 0 && random < 0.25){
                x = x + 1;
                steps++;
            }
            if(random > 0.25 && random < 0.50){
                x = x - 1;
                steps++;
            }
            if(random > 0.50 && random < 0.75){
                y = y + 1;
                steps++;
            }
            if(random > 0.75 && random < 1){
                y = y - 1;
                steps++;

            }
            distance = Math.abs(x+y);
        }
        sumOfSteps = sumOfSteps + steps;
    }
    averageSteps = (double)(sumOfSteps / trials);
    System.out.println("average steps" + averageSteps);
}}

Here is my run program:

~/IdeaProjects/MonteCarloSimulation/src$ java RandomWalkers 5 1000000
average steps27.0
~/IdeaProjects/MonteCarloSimulation/src$ java RandomWalkers 5 1000000
average steps7.0

You can see that the average steps quite differ a lot from the example run-time model that the instructor gave to me. Besides that,my average steps are always integer data type,even if I set the averageSteps variable should be double.

arshovon
  • 13,270
  • 9
  • 51
  • 69
Tr909
  • 31
  • 3

1 Answers1

1

There are a few issues in your code. First of all, you need to reset the x, y, distance and steps variables after each trial. Or, even better, to define and initialise them to 0 at the beginning of the for loop, like so:

for(int i = 1 ; i <=trials ; i++){
        int x = 0;
        int y = 0;
        int distance = 0;
        int steps = 0;

        [...]
}

Secondly, your implementation of the Manhattan distance is not correct. That line should read

distance = Math.abs(x) + Math.abs(y);

About the lack of significant digits after the decimal separator, in your code you cast to double the result of an integer operation, which returns an integer. You need to cast one of the integers to a double before performing the actual mathematical operation, like so:

averageSteps = ((double)sumOfSteps / trials);
lr1985
  • 1,085
  • 1
  • 9
  • 21