Exercise 11.4

Exercise 11.4#

Question 1#

Write a script to simulate a coin toss. Prompt the user to enter “heads” or “tails” into the terminal, then generate a random number (from a selection of 2 numbers) to determine if they won the coin toss.

If the user wins, print the message “Congratulations! You won!”. If the user loses, print out the messages “Sorry, it was heads.” or “Sorry, it was tails.” where relevant.

Question 2#

A random walk is a simple simulation where random steps are taken in a range of possible directions/sizes.

Consider the scenario where a person (walker) is walking in a straight line. They take steps of random sizes in the range of 0.5-1m.

Create a random walk simulation that tells you how many steps the walker takes to reach (or pass) a distance of 100m. You can assume that the step size follows a uniform probability distribution function.

Question 3#

Now suppose that the walker from Question 4 were to choose a random direction for each step, maintaining the same range of step sizes as before.

Write a script that takes a user input for how many steps the walker should take, then simulates the random walk mentioned above. When the walk is complete, plot the path that was taken.

You can assume that there is no bias for which direction the walker picks for each step. How do you ensure this uniform distributions of “directions”?