Question
Java program Write a int method named randomWalk(int x) that performs a random one-dimensional walk, reporting each position reached and the maximum position reached during
Java program
Write a int method named randomWalk(int x) that performs a random one-dimensional walk, reporting each position reached and the maximum position reached during the walk.
To do a "random walk", you begin at position 0. Then, on each step, you should either increase or decrease the position by 1 (with equal probability). The walk stops when x or -x is reached.
It should then print out the steps and return the number of steps that it took.
Once your method works, make a main that calls your function for x = 30.
*********** Sample Output *********************************************************************************************
The output should look like this for randomWalk(10):
position = 0 position = 1 position = 0 position = -1 position = -2 position = -1 position = -2 position = -3 position = -4 position = -5 position = -4 position = -5 position = -6 position = -7 position = -8 position = -7 position = -8 position = -9 position = -10 Steps : 19
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started