Question: Write a Java class RandomWalk with a main method to simulate the so-called Drunken Sailors Walk. Here is the setup: A drunken man leaves a
Write a Java class RandomWalk with a main method to simulate the so-called Drunken Sailors Walk. Here is the setup:
A drunken man leaves a pub and walks out onto College Avenue, bound for his apartment 100 feet from the bars front door. He is so drunk that he cant control in which direction he takes a step, and randomly takes a step, forward towards the apartment or backward in the other direction, with a probability of each equal to . Assume that the length of his steps is fixed at 2 feet, and he makes a step every 5 seconds.
Assume that a police cruiser is circling the College Ave./Atherton St./Beaver Ave. loop every 5 minutes, and that if they see him in basically the same spot more than 5 times, theyll stop and check him out, and ultimately arrest him for public drunkenness. You can calculate that this gives him 30 minutes to get to his apartment, and thus has 30 minutes * 60 seconds/minute / 5 seconds/step = 360 steps in 30 minutes. The goal of your simulation is to determine the probability that after 360 steps or less, he makes it to his apartment before he is picked up by the police. Or you could look at it inversely as 1 minus the probability that the police will catch him before he makes it to his apartment.
There are a number of ways to solve this problem, but the way well use here is to write a program to run what is known as a Monte Carlo simulation. You want to simulate taking 360 2-foot steps either forward or backward using a random number generator. One way to simulate each 2-foot step is to use the Math.random() method used in the class exercise last Monday 10/5 (GuessNumber.java program is in source code directory). Remember that Math.random() returns a pseudorandom double number between 0.0 (inclusive) and 1.0 (exclusive). So about half the time, the result should be be between 0 and 0.499999999, and the other half of the time it should be between 0.5 and 0.999999999. Well do a class exercise on Monday 10/12 to develop and test this random number generator.
Now you simply need to sum that result 360 times and then run it over and over again until you get a probability estimate that gradually settles down to a fixed value this is called convergence of the algorithm.
Run this same simulation over and over again until youre convinced you have a reasonable estimate of the probability that the pub patron gets home before the 30 minutes are up. Use the same approach as in the Monday 10/12 class exercise to give the user a way to interact to decide if the probability estimate is good enough. As I stated there, Id break every so many times, display the number of repetitions and the estimated probability, and prompt the user to input whether or not they want to continue.
Suggestions:
As you're working through these exercises, I strongly recommend that you visit the Java API documentation: http://docs.oracle.com/javase/8/docs/api/. In particular, I recommend you look at the java.lang package documentation for methods in the String and Math classes. Post on the forum if you have trouble, we'll try to answer questions, short of doing the programming for you.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
