Question
A drunk man is 100 steps away from his home. Please write a C++ function that takes his current position, and updates where he is
A drunk man is 100 steps away from his home. Please write a C++ function that takes his current position, and updates where he is next. You can use an integer function which returns his new position or a void function with a pass-by reference. Either way is fine; it is up to you to choose how you want to do it. int randomStep(int position) OR void randomStep(int& position) His probability of taking a step toward his home (position+1) is 2/3 and taking a step away (position-1) is 1/3. The function should generate a random number and determine whether he takes a step toward or away from his home, giving you his new position. Using this function, write a main program that simulates his random walk starting at position equals 0 until he reaches his home (position equals 100). The program should count and display how many steps he took to reach his home. (Hint: If you do the probability analysis, youll find that the average number of steps it takes for him to get home is 300, so your output should be reasonably close to that)
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