The Drunkard’s Walk 🟠
Contents
The Drunkard’s Walk 🟠#
A drunken sailor returns to his ship via a bridge 70 metre long
and 14 metre wide
.
What is the probability that he returns safely to his ship, considering:
The Bridge:
Length (x direction) = 70 m
Width (y direction) = 14 m
The drunk has a:
50% chance to step forward (x direction)
25% chance to step left (positive y direction)
25% chance to step right (negative y direction)
Step length: 70 cm
Boundary conditions:
Sailor’s starting x position: 0
Sailor’s starting y position: Width/2
open y boundaries (the sailor may fall off the bridge)
Write a function that works as a single step simulator. Then, write another function (walk simulator) that calls the single step simulator several times to simulate an entire Sailor’s trajectory, which can end with the sailor falling into the
water
or reaching theship
. Finally, write a probability function that calls the walk simulator several times to determine the approximate probability that the sailor will arrive on the ship.
Rules#
Step simulator#
Function returns a
string
value betweenforward
,left
andright
.Step is not “fair”:
50%
forward
25%
left
25%
right
You can use any
random function
from therandom
module.
Walk simulator#
Function returns a
string
value betweenwater
andship
.You should call the
step_simulator()
function.
Probability calculator#
Function returns a
float
value between0
and1
.Avoid using any knowledge of combinatorics to solve this problem. Instead, take advantage of the fact that the machine can simulate millions of steps/walks in a very short time.
Note
Since this is based on random draws, the probability will be slightly different each time the code is run.