Description
A drunkard’s walk. A drunkard begins walking aimlessly, starting at a
lamp post. At each step, the drunkard forgets where he or she is, and
takes one step at random, either north, east, south, or west, with
probability 25%. How far will the drunkard be from the lamp post after N
steps?
Write a program that simulates the motion of a random walker for N steps.
Your program should ask the user for an input which should be an integer
number (examples: 1, 10, 100…) and should work for values of steps from
1 to a large number. Your program should print an error message if an
invalid number (negative) is entered.
After each step, print the location of the random walker, treating the lamp
post as the origin (0, 0). When the walker concludes print the square of
the final *squared* distance from the origin (x2 + y2).
Example printouts could look like:
N = 10 N = 15
(0, -1) (0, 1)
(0, 0) (-1, 1)
(0, 1) (-1, 2)
(0, 2) (0, 2)
(-1, 2) (1, 2)
(-2, 2) (1, 3)
(-2, 1) (0, 3)
(-1, 1) (-1, 3)
(-2, 1) (-2, 3)
(-3, 1) (-3, 3)
distance = 10 (-3, 2)
(-4, 2)
(-4, 1)
(-3, 1)
(-3, 0)
distance = 9
2
Duplicate and modify your program to now take two prompts:
The first is still N – the number of steps the walker will take.
The second is T – the number of trials, or the number of times you will
measure the walker’s distance.
Your modified program should print out the average distance walked in N
steps across the T trials.
N=100 T=10000 N=400 T=2000
mean sq. dist = 101.446 mean sq. dist = 383.12
N=100 T=10000 N=800 T=5000
mean sq. dist = 99.1674 mean sq. dist = 811.8264
N=200 T=1000 N=1600 T=100000
mean sq. dist = 195.75 mean sq. dist = 1600.131
Note: these are examples! Your code will not produce these exact results
but should only have a single line of output that says
mean sq. distance = ________
As N increases, we expect the random walker to end up further and
further away from the origin. But how much further? Use your program to
formulate a hypothesis as to how the mean squared distance grows as a
function of N. Use a variety of values for T (as shown above) for testing
the program, but for this analysis, use T = 100,000 trials to get a
sufficiently accurate estimate.
Submit two separate pieces of code labeled:
152_assignment3_1.java (or .pde)
152_assignment3_2.java (or .pde)
Each program must include (in a comment) your name, the date, and the
course information. They may contain other comments.