Question

In: Computer Science

"The Drunkard’s Walk. A drunkard in a grid of streets randomly picks one of four directions...

"The Drunkard’s Walk. A drunkard in a grid of streets randomly picks one of four directions and stumbles to the next intersection, then again randomly picks one of four directions, and so on. You might think that on average the drunkard doesn’t move very far because the choices cancel each other out, but that is actually not the case."

for java 1.7

Solutions

Expert Solution

Solution:

import java.util.*;
// declare class
public class drunkardWalk{
        //declare the main method
        public static void main(String[] args) {
                 
                // welcome the user
                System.out.println("If a drunk person at location (x,y) randomly travels i various directions in a grid city. What happens after 100 random choices.");

                Random randomGenerator = new Random();
                int x = 0;
                int y = 0;
                double distance = 0;
                for (int decisionCounter = 0; decisionCounter < 100; decisionCounter ++){
                        int direction = randomGenerator.nextInt(4) + 1 ;

                         switch (direction) {
                         case 1: x = x + 1; //going east one block
                                break;
                         case 2: x = x + -1;//going west one block
                                break;
                                
                         case 3: y = y + 1;//going north one block
                                break;
                                
                         case 4: y = y - 1;//going south one block\
                                break;
                         }//switch END
                        
                }//loop END
                // d= sqrt of [(x - 0)^2 + (y - 0)^2]
                distance = Math.sqrt(((x * x) + (y * y)));
                
                
                
                System.out.printf("If the city blockes are one mile by one mile then the drunk person has traveled %4.2f", distance);
                System.out.println(" miles away from his original location by walking 100 miles. ");
                  
  
        }//main END
}//class END

Please give thumbsup or do comment in case of any query. Thanks.


Related Solutions

Pablo randomly picks three marbles from a bag of nine marbles (four red ones, three green...
Pablo randomly picks three marbles from a bag of nine marbles (four red ones, three green ones, and two yellow ones). How many outcomes are there in the event that Pablo picks one marble of each color? outcomes
If a person randomly picks two different months, what is the probability that the correlation between...
If a person randomly picks two different months, what is the probability that the correlation between the two months is negative?
Task 2C: User picks an integer in a range and computer guesses by randomly This approach...
Task 2C: User picks an integer in a range and computer guesses by randomly This approach is similar to Task 2B. The only difference is, the computer will generate a random integer in the range as a guess. In this strategy, a guess is a random int in a given range. Hence, for the same range and same answer, the number of guesses will be different. For example, suppose the range is in [0, 10] and the answer is 7....
compare and contrast the managerial grid and the four framework approach
compare and contrast the managerial grid and the four framework approach
Write a program in c++ that picks four cards from a deck of 52 cards and...
Write a program in c++ that picks four cards from a deck of 52 cards and computes the sum of the four cards. An Ace, King, Queen, and Jack represent 1, 13, 12, and 11, respectively. Your program should display the number of picks that yields the sum of 24. You are not allowed to use arrays. Declare four variables to hold the card values.
a) A treasure map gives the following directions: (1) Walk fifty meters at 30 degrees north...
a) A treasure map gives the following directions: (1) Walk fifty meters at 30 degrees north or east from the old oak tree. (2) Turn 45 degrees to your left (you should now be facing 75degrees north of east) and walk another fifty meters. You will find the treasure buried under a rock. What straight-line path would take you directly from the old oak tree to the rock with the treasure buried under it? b) You followed the first direction...
Four couples go to dinner together. The waiter seats the men randomly on one side of...
Four couples go to dinner together. The waiter seats the men randomly on one side of the table and the women randomly on the other side of the table. Find the expected value and variance of the number of couples who are seated across from each other. Please help without using equation that has 1/x! and (-1)^k/k Thanks!
Four long straight wires are laid out on a grid as follows: Wire 1 is carrying...
Four long straight wires are laid out on a grid as follows: Wire 1 is carrying a current of 1.00 A upward on the page along the line x = 0 Wire 2 is carrying a current of 2.00 A rightward on the page along the line y = 4.00 m Wire 3 is carrying a current of 3.00 A downward on the page along the line x = 5.00 m Wire 4 is carrying a current of 4.00 A...
Use the pigeonhole principle to show that if one picks nine numbers between 2 (inclusive), at...
Use the pigeonhole principle to show that if one picks nine numbers between 2 (inclusive), at least two of the numbers chosen must have a common divisor d ≥ 2
1.) Using excel. A random number generator picks a number from one to nine in a...
1.) Using excel. A random number generator picks a number from one to nine in a uniform manner. X ~ _________ Graph the probability distribution. f(x) = _________ μ = _________ σ = _________ P(3.5 < x < 7.25) = _________ P(x > 5.67) P(x > 5|x > 3) = _________ Find the 90th percentile. 2) using excel A subway train on the Red Line arrives every eight minutes during rush hour. We are interested in the length of time...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT