1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Tasya [4]
4 years ago
14

Two-dimensional random walk (20 points). 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.

Computers and Technology
1 answer:
natka813 [3]4 years ago
4 0

Answer:

The following code in the explanation does the job for you. Here I have made use of Random class of Java to generate random numbers.

Explanation:

Code:

import java.util.*;

class RandomWalker {

  public static void main(String args[]){

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

      int x = 0;

      int y = 0;

      Random random = new Random();

      for(int i = 0; i < n; i++){

          int tmp = random.nextInt(4);

          if(tmp == 0){

              x++;

          }

          if(tmp == 1){

              y++;

          }

          if(tmp == 2){

              y--;

          }

          if(tmp == 3){

              x--;

          }

          System.out.println("(" + x + "," + y + ")");

      }

      int distance = x*x + y*y;

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

  }

}

You might be interested in
Which of these statements best describes an application programming interface?
TiliK225 [7]
<span>It is a set of commands, provided by the manufacturer of an operating system, which allows programmers to create applications.</span>
5 0
3 years ago
Select the correct answer.
Kobotan [32]
I think that it's OD
7 0
3 years ago
Does anyone know the code for codeHS 5.4.7 teenagers?
Over [174]

Answer:

function start(){

var age = readInt("Age: ");

if(age == 15){

println("Yes, you are a teenager.");

}else{

println("No, you are not a teenager.");

}

}

Explanation:

6 0
3 years ago
3) An algorithm has a run time of O(nk ) for some integer k. On an input of size 500, the algorithm takes 16 seconds to run. On
lukranit [14]

Answer:

The value of k is 4

Explanation:

Solution

Given that:

k = integer

Input size = 500

The algorithm takes a run of = 16 seconds

Input size = 750

The algorithm takes a run of = 81 seconds

Now,

We have to determine the value of k

The equation is shown below:

(500)^k /16 = (750) ^k /81

Thus

(750/500)^ k = 81/16

= (3/2)^k

=(3/2)^ 4

k is = 4

6 0
3 years ago
Definition of the term social factor and explain why texting while driving can be classified as a social factor causing accident
sergij07 [2.7K]
Social factor - Something that affects you!
Texting and driving is classified as a social factor because your phone is the reason why you crashed your car.
8 0
3 years ago
Other questions:
  • Marco makes $65,000 a year as a graphic designer. He makes a charitable donation of $1,000 each year. He also has $5,000 of busi
    12·2 answers
  • Modern car odometers can record up to a million miles driven. What happens to the odometer reading when a car drives beyond its
    8·1 answer
  • How do Filament 3D printers build a model?
    8·1 answer
  • A data mart differs from a data warehouse in that: Group of answer choices it deals with a particular component or functional ar
    13·1 answer
  • What is the output of the following code fragment? int i = 1; int sum = 0; while (i &lt;= 15) { sum = sum + i; i++; } System.out
    5·1 answer
  • What types of projects require collaboration? en360
    15·1 answer
  • What file name would allow you to easily find and edit your document later in Word Online?
    14·2 answers
  • How does porter’s competitive forces model help companies develop competitive strategies using information systems?
    13·1 answer
  • Mention how to install antivirus software in your computer, either by following the instructions given on installation CDs or we
    6·1 answer
  • 15. It is the process of capturing data or translating information to recording format
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!