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]
3 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]3 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
What is this tool called?
kondor19780726 [428]
Is there a picture????????
4 0
3 years ago
Read 2 more answers
write a program that keeps names and email addresses in a dictionary as key-value pairs. the program should display a menu that
erma4kov [3.2K]

To write a program that keeps names and email addresses in a dictionary as key-value pairs check the code given below.

<h3>What is key-value pairs?</h3>

In a key-value pair, two related data elements are combined: a value, which is a variable that belongs to the set (for example, male/female, green, 100), and a key, which is a constant that defines the data set (for example, gender, color, price).

A key-value pair could look something like this when fully formed:

gender = male

color = green

price > 100

↓↓↓//<u>Python code</u>//↓↓↓

import pickle

import sys

try:

   f=open('email.dat','rb')

   d=pickle.load(f)  

   f.close()

     

except:    

   d={}

while True:

   print('\n1. Find a email address')

   print('2. Add name and email address')

   print('3. Change an email address')

   print('4. Delete an email address')

   print('5. Exit\n')

   choice=input('\nEnter a choice: ')

   if choice:

       choice=int(choice)

   else:

       print('\nEnter a number')

       continue    

   if choice == 1:

       while True:

           name=input('\nEnter the name ')

           if name:

               if name in d:

                   print('\n%s is the email id of %s \n' % (d[name],name))

                   break

               else:

                   print('\n Email not found \n')

                   break

           else:

               print('\nName cannot be empty\n')

               continue

           

   elif choice==2:

       while True:            

       

           name=input('\nEnter the name ')

           if name:

               break;

           else:

               print('\nName cannot be empty \n')

               continue

       while True:            

       

           email=input('\nEnter the email address ')

           if email:

               d[name]=email

               break

           else:

               print('\nEmail cannot be empty\n')

               continue

           

   elif choice==3:

       while True:            

       

           name=input('\nEnter the name to change the email address ')

           if name:

               if name in d:

                   email=input('\nEnter the new email address ')

                   d[name]=email

                   print('\nEmail address changed \n')

                   break;

               else:

                   print('\nName not found \n')

                   break

           else:

               print('\nName cannot be empty \n')

               continue

           

   elif choice == 4:

       while True:            

       

           name=input('\nEnter the name to remove ')

           if name:

               if name in d:

                   del d[name]

                   print('\nName and Email address removed \n ')

                   break;

               else:

                   print('\nName not found \n')

                   break

           else:

               print('\nName cannot be empty\n')

               continue

   elif choice == 5:

       

       f=open('email.dat','wb')

       pickle.dump(d,f)

       f.close()

       sys.exit()

   else:

       print('\nEnter a valid choice ')    

Learn more about key-value pair

brainly.com/question/29414672

#SPJ1

3 0
1 year ago
Lori wants to set up a SOHO network in her apartment. The apartment comes with a Gigabit Ethernet network already installed. Lor
Yanka [14]

Connection to the gigabit ethernet network must be done with Cat6 Cable. Using the wireless ethernet router as the network device and connecting the cable to the printer must be done with Cat5e Cable.

We can arrive at this answer because:

  • The Cat6 Cable will be responsible for establishing a bridge between the gigabyte Ethernet and the network device, allowing data delivery to be made between the two systems.
  • This connection must be made with a wireless device, to keep it more stabilized and it needs, mainly, for the notebook to receive the internet signal. This will be done using the wireless ethernet router.
  • The printer needs a softer, less rigid connection, so a Cat5e cable will be a convenient option.

In this case, we can see that using these devices will allow Lori to have a more stable and efficient connection to meet her needs.

More information on network connection at the link:

brainly.com/question/8118353

5 0
2 years ago
Read 2 more answers
Please help guys I'm so lost ​
PilotLPTM [1.2K]

Answer:

The correct answer is C ( W * 5 )

4 0
3 years ago
Read 2 more answers
Uso de las redes sociales como el uso de una impresora en casa se satisfacen estás tres etapas
Svetradugi [14.3K]

Question: what question is this?

7 0
2 years ago
Other questions:
  • Florida convicted __________ people of DUI in a one-year period from 2009 to 2010.
    15·1 answer
  • Why do you think LinkedIn has become so popular?
    6·1 answer
  • Given four values representing counts of quarters, dimes, nickels and pennies, output the total amount as dollars and cents. Out
    10·1 answer
  • Which style of leadership would be most helpful to Charles in the following situation? Charles has just started an internship at
    15·1 answer
  • When cleaning a computer, you need only worry about what you can see.
    11·1 answer
  • Why might it be a good idea to choose a bus topology?
    6·2 answers
  • Jenis jenis perangkat keras?
    14·1 answer
  • Write a method that computes the sum of the digits in an integer. Use the following method header: public static int sumDigits(l
    5·1 answer
  • Give one example of where augmented reality is used​
    11·2 answers
  • ___ is a form of electronic money that is decentralized and whose transactions are encrypted, processed, and recorded via peer-t
    10·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!