Random Walk
This program will give you practice with while loops, Random and using objects. Since this project doesn’t have clear objects to create like dice or games, you will be writing everything using static methods. I want you to get a feel for when it’s more appropriate to use classes that can be instantiated and when it’s more appropriate to use static methods, so make sure you’re thinking about this. Optionally, you can create a RandomWalker class. This will allow you to have more than one RandomWalker on the screen, which can make your program more interesting. You will be using the Point class you wrote earlier.
In this program, you will be simulating a “random walk” from the center of a circle out to its perimeter. Read more about random walks on Wikipedia. You will be drawing this walk on a DrawingPanel to produce output like the following:


Your program should allow the user to produce multiple walks. Here is a sample interaction:
Radius? 50 I escaped in 2468 move(s). Walk again (yes/no)? y
Radius? 35 I escaped in 987 move(s). Walk again (yes/no)? YES
Radius? 100 I escaped in 13713 move(s). Walk again (yes/no)? n
Total walks = 3 Total steps = 17168 Best walk = 987
This interaction should have drawn three different random walks on the same 500×500 DrawingPanel, erasing in between each run. You may assume that users type in integers when prompted for a radius and a string when prompted whether or not to play again.
As usual, you will be graded on style, program structure (methods methods methods!!) and reduced redundancy. Make sure main is a good description of the program.
Hints
- Use a while loop to do repetition. You should use your Point class’s distance method to know when to stop…
- Work on a text version of this program before adding graphics: just print the new coordinates of the randomly walking point every time you repeat
- Start by writing a program that just performs a single random walk (doesn’t prompt the user to repeat)
- Test with small radius values so it doesn’t take forever
- Draw a single pixel by filling a 1×1 rectangle. For example, to draw a pixel a Point variable p’s coordinates, you’d say:
g.fillRect(p.x, p.y, 1, 1); // draw one pixel at p's position
- My solution uses 4 methods and is about 100 lines
- You must have a method to perform just one random walk
- You must have a method to report statistics about the walks
Extensions
- Make each step a random color
- Change color every 15 steps
- Animate the random walk by using the DrawingPanel’s sleep method with a number of ms as a parameter
- Put all this in a RandomWalker class with color and name fields. Create five RandomWalkers of different colors and make them race! Report who has won at the end.






