A two-dimensional random walk simulates the behavior of a particle moving in a grid of points. At each step, the random walker moves north, south, east, or west with probability equal to 1/4, independent of previous moves. Write a program RandomWalker.java that takes an int command-line argument n and simulates the motion of a random walk for n steps. Print the location at each step (including the starting point), treating the starting point as the origin (0, 0). Also, print the square of the final squared Euclidean distance from the origin as double. Note: you do not need arrays for this problem, just keep track of the x and y coordinates during the random walk.

Respuesta :

Answer:

public class RandomWalker

{

             public static void main(String[] args) {

                 

                 int randomNumber =0;

                            double distanceBetweenPoints = 0.0;

                 int posX =0, posY=0;

                            int newPosX =posX;

                            int newPosY =posY;

             

                            //read the command line argument

                            int number = Integer.parseInt(args[0]);

       

       int count =0;

       

       System.out.println("(" + newPosX + "," + newPosY + ")"); //print the origin point

       while(count < number)

       {

           //to get a number between 1 to 4

           randomNumber = (int)(System.currentTimeMillis()%(3*count+11));

           randomNumber = (randomNumber %4)+1;

           

           //if randomNumber is one then move east, 2- Move west, 3-Move north, else Move south

           if (randomNumber == 1)

               newPosX += 1;

           else if(randomNumber == 2)

               newPosX-=1;

           else if (randomNumber == 3)

               newPosY+=1;

           else

               newPosY-=1;

           //display the new point

           System.out.println("(" + newPosX + "," + newPosY + ")");

           count++;

       }

                           

                            //Euclidean Squared distance Between Points

                            distanceBetweenPoints = (newPosX-posX)*(newPosX-posX) + (newPosY-posY)*(newPosY-posY);

                            System.out.println("Squared distance = " + distanceBetweenPoints);

                           

             }

}

Explanation:

In this exercise we have to use the knowledge of computational language in C++ to write the code:

We have that can be found in the attached image.

To make it simpler we find the code below as:

public class RandomWalker

{

 public static void main (String[]args)

 {

   int randomNumber = 0;

   double distanceBetweenPoints = 0.0;

   int posX = 0, posY = 0;

   int newPosX = posX;

   int newPosY = posY;

   //read the command line argument

   int number = Integer.parseInt (args[0]);

   int count = 0;

     System.out.println ("(" + newPosX + "," + newPosY + ")"); //print the origin point

   while (count < number)

     {

//to get a number between 1 to 4

randomNumber = (int) (System.currentTimeMillis () % (3 * count + 11));

randomNumber = (randomNumber % 4) + 1;

//if randomNumber is one then move east, 2- Move west, 3-Move north, else Move south

if (randomNumber == 1)

  newPosX += 1;

else if (randomNumber == 2)

  newPosX -= 1;

else if (randomNumber == 3)

  newPosY += 1;

else

  newPosY -= 1;

//display the new point

System.out.println ("(" + newPosX + "," + newPosY + ")");

count++;

     }

   //Euclidean Squared distance Between Points

   distanceBetweenPoints =

     (newPosX - posX) * (newPosX - posX) + (newPosY - posY) * (newPosY -posY);

   System.out.println ("Squared distance = " + distanceBetweenPoints);

 }

}

See more about C++ at brainly.com/question/13138795

Ver imagen lhmarianateixeira