Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USING PYTHON PROGRAMMING LANGUAGE Write a program that simulates a 2D drunkard’s

ID: 3873476 • Letter: U

Question

USING PYTHON PROGRAMMING LANGUAGE

Write a program that simulates a 2D drunkard’s walk with 100 steps. That is, write a program that starts at (0, 0), and at each step of the program, changes that position by either +1 in the x-direction, 1 in the x-direction, +1 in the y-direction, or 1 in the y-direction, each possibility happening with probability 1/4.

For example, the first few steps might be (0,0) (0,1) (1,1) (2,1) (2,0) (1,0) (2,0)

Have the program print out the location (that is, the x-coordinate and y-coordinate) after 100 steps.

Explanation / Answer

Python 2.7 code:

import random
x = 0;#initial location x coordinate
y = 0;#initial location y coordinate
for i in range(0,100):
print "After ", (i+1), " iterations location = ", "(",x , ",",y, ")"
random_num = random.uniform(0, 1); # generate a random number in range(0,1) to take a step
if(random_num <= 0.25):
  x = x+1
elif(random_num > 0.25 and random_num <= 0.50):
  x = x-1
elif(random_num > 0.50 and random_num <= 0.75):
  y = y + 1
else:
  y = y - 1

Sample Output:

After 1 iterations location = ( 0 , 0 )
After 2 iterations location = ( -1 , 0 )
After 3 iterations location = ( -1 , 1 )
After 4 iterations location = ( -1 , 2 )
After 5 iterations location = ( -2 , 2 )
After 6 iterations location = ( -1 , 2 )
After 7 iterations location = ( -2 , 2 )
After 8 iterations location = ( -1 , 2 )
After 9 iterations location = ( -1 , 1 )
After 10 iterations location = ( -1 , 2 )
After 11 iterations location = ( -2 , 2 )
After 12 iterations location = ( -2 , 1 )
After 13 iterations location = ( -2 , 0 )
After 14 iterations location = ( -1 , 0 )
After 15 iterations location = ( -1 , 1 )
After 16 iterations location = ( -1 , 0 )
After 17 iterations location = ( -2 , 0 )
After 18 iterations location = ( -2 , 1 )
After 19 iterations location = ( -3 , 1 )
After 20 iterations location = ( -2 , 1 )
After 21 iterations location = ( -2 , 0 )
After 22 iterations location = ( -2 , 1 )
After 23 iterations location = ( -3 , 1 )
After 24 iterations location = ( -3 , 2 )
After 25 iterations location = ( -3 , 3 )
After 26 iterations location = ( -3 , 2 )
After 27 iterations location = ( -4 , 2 )
After 28 iterations location = ( -4 , 3 )
After 29 iterations location = ( -5 , 3 )
After 30 iterations location = ( -5 , 2 )
After 31 iterations location = ( -5 , 3 )
After 32 iterations location = ( -5 , 4 )
After 33 iterations location = ( -6 , 4 )
After 34 iterations location = ( -5 , 4 )
After 35 iterations location = ( -4 , 4 )
After 36 iterations location = ( -4 , 3 )
After 37 iterations location = ( -3 , 3 )
After 38 iterations location = ( -3 , 2 )
After 39 iterations location = ( -4 , 2 )
After 40 iterations location = ( -3 , 2 )
After 41 iterations location = ( -2 , 2 )
After 42 iterations location = ( -1 , 2 )
After 43 iterations location = ( 0 , 2 )
After 44 iterations location = ( -1 , 2 )
After 45 iterations location = ( 0 , 2 )
After 46 iterations location = ( 1 , 2 )
After 47 iterations location = ( 1 , 1 )
After 48 iterations location = ( 2 , 1 )
After 49 iterations location = ( 1 , 1 )
After 50 iterations location = ( 0 , 1 )
After 51 iterations location = ( 1 , 1 )
After 52 iterations location = ( 2 , 1 )
After 53 iterations location = ( 2 , 0 )
After 54 iterations location = ( 3 , 0 )
After 55 iterations location = ( 4 , 0 )
After 56 iterations location = ( 5 , 0 )
After 57 iterations location = ( 5 , -1 )
After 58 iterations location = ( 5 , 0 )
After 59 iterations location = ( 4 , 0 )
After 60 iterations location = ( 5 , 0 )
After 61 iterations location = ( 5 , 1 )
After 62 iterations location = ( 4 , 1 )
After 63 iterations location = ( 4 , 2 )
After 64 iterations location = ( 4 , 1 )
After 65 iterations location = ( 4 , 0 )
After 66 iterations location = ( 4 , -1 )
After 67 iterations location = ( 3 , -1 )
After 68 iterations location = ( 4 , -1 )
After 69 iterations location = ( 4 , 0 )
After 70 iterations location = ( 5 , 0 )
After 71 iterations location = ( 6 , 0 )
After 72 iterations location = ( 5 , 0 )
After 73 iterations location = ( 4 , 0 )
After 74 iterations location = ( 4 , -1 )
After 75 iterations location = ( 4 , -2 )
After 76 iterations location = ( 5 , -2 )
After 77 iterations location = ( 4 , -2 )
After 78 iterations location = ( 3 , -2 )
After 79 iterations location = ( 2 , -2 )
After 80 iterations location = ( 2 , -1 )
After 81 iterations location = ( 2 , -2 )
After 82 iterations location = ( 3 , -2 )
After 83 iterations location = ( 3 , -1 )
After 84 iterations location = ( 2 , -1 )
After 85 iterations location = ( 1 , -1 )
After 86 iterations location = ( 1 , -2 )
After 87 iterations location = ( 0 , -2 )
After 88 iterations location = ( -1 , -2 )
After 89 iterations location = ( -2 , -2 )
After 90 iterations location = ( -2 , -1 )
After 91 iterations location = ( -2 , 0 )
After 92 iterations location = ( -2 , 1 )
After 93 iterations location = ( -3 , 1 )
After 94 iterations location = ( -4 , 1 )
After 95 iterations location = ( -4 , 0 )
After 96 iterations location = ( -4 , 1 )
After 97 iterations location = ( -3 , 1 )
After 98 iterations location = ( -4 , 1 )
After 99 iterations location = ( -5 , 1 )
After 100 iterations location = ( -4 , 1 )

C:UsersAkashDesktopchegg>python test.py
After 1 iterations location = ( 0 , 0 )
After 2 iterations location = ( -1 , 0 )
After 3 iterations location = ( -1 , 1 )
After 4 iterations location = ( -2 , 1 )
After 5 iterations location = ( -1 , 1 )
After 6 iterations location = ( -1 , 2 )
After 7 iterations location = ( -1 , 1 )
After 8 iterations location = ( -1 , 2 )
After 9 iterations location = ( -1 , 3 )
After 10 iterations location = ( 0 , 3 )
After 11 iterations location = ( 0 , 4 )
After 12 iterations location = ( 0 , 3 )
After 13 iterations location = ( 1 , 3 )
After 14 iterations location = ( 2 , 3 )
After 15 iterations location = ( 3 , 3 )
After 16 iterations location = ( 2 , 3 )
After 17 iterations location = ( 2 , 4 )
After 18 iterations location = ( 2 , 5 )
After 19 iterations location = ( 1 , 5 )
After 20 iterations location = ( 2 , 5 )
After 21 iterations location = ( 2 , 4 )
After 22 iterations location = ( 3 , 4 )
After 23 iterations location = ( 3 , 5 )
After 24 iterations location = ( 3 , 6 )
After 25 iterations location = ( 4 , 6 )
After 26 iterations location = ( 4 , 5 )
After 27 iterations location = ( 3 , 5 )
After 28 iterations location = ( 3 , 6 )
After 29 iterations location = ( 3 , 5 )
After 30 iterations location = ( 3 , 6 )
After 31 iterations location = ( 4 , 6 )
After 32 iterations location = ( 4 , 7 )
After 33 iterations location = ( 5 , 7 )
After 34 iterations location = ( 5 , 6 )
After 35 iterations location = ( 4 , 6 )
After 36 iterations location = ( 3 , 6 )
After 37 iterations location = ( 3 , 5 )
After 38 iterations location = ( 2 , 5 )
After 39 iterations location = ( 1 , 5 )
After 40 iterations location = ( 1 , 6 )
After 41 iterations location = ( 0 , 6 )
After 42 iterations location = ( 0 , 7 )
After 43 iterations location = ( 1 , 7 )
After 44 iterations location = ( 2 , 7 )
After 45 iterations location = ( 2 , 8 )
After 46 iterations location = ( 2 , 9 )
After 47 iterations location = ( 2 , 10 )
After 48 iterations location = ( 3 , 10 )
After 49 iterations location = ( 4 , 10 )
After 50 iterations location = ( 5 , 10 )
After 51 iterations location = ( 5 , 11 )
After 52 iterations location = ( 5 , 12 )
After 53 iterations location = ( 4 , 12 )
After 54 iterations location = ( 5 , 12 )
After 55 iterations location = ( 4 , 12 )
After 56 iterations location = ( 3 , 12 )
After 57 iterations location = ( 2 , 12 )
After 58 iterations location = ( 2 , 13 )
After 59 iterations location = ( 1 , 13 )
After 60 iterations location = ( 1 , 12 )
After 61 iterations location = ( 1 , 11 )
After 62 iterations location = ( 2 , 11 )
After 63 iterations location = ( 2 , 10 )
After 64 iterations location = ( 3 , 10 )
After 65 iterations location = ( 4 , 10 )
After 66 iterations location = ( 5 , 10 )
After 67 iterations location = ( 5 , 9 )
After 68 iterations location = ( 5 , 10 )
After 69 iterations location = ( 6 , 10 )
After 70 iterations location = ( 5 , 10 )
After 71 iterations location = ( 4 , 10 )
After 72 iterations location = ( 5 , 10 )
After 73 iterations location = ( 5 , 11 )
After 74 iterations location = ( 5 , 10 )
After 75 iterations location = ( 4 , 10 )
After 76 iterations location = ( 4 , 11 )
After 77 iterations location = ( 4 , 10 )
After 78 iterations location = ( 4 , 9 )
After 79 iterations location = ( 4 , 10 )
After 80 iterations location = ( 4 , 9 )
After 81 iterations location = ( 3 , 9 )
After 82 iterations location = ( 3 , 8 )
After 83 iterations location = ( 3 , 9 )
After 84 iterations location = ( 2 , 9 )
After 85 iterations location = ( 1 , 9 )
After 86 iterations location = ( 1 , 8 )
After 87 iterations location = ( 2 , 8 )
After 88 iterations location = ( 2 , 7 )
After 89 iterations location = ( 2 , 8 )
After 90 iterations location = ( 2 , 9 )
After 91 iterations location = ( 1 , 9 )
After 92 iterations location = ( 1 , 10 )
After 93 iterations location = ( 2 , 10 )
After 94 iterations location = ( 1 , 10 )
After 95 iterations location = ( 1 , 11 )
After 96 iterations location = ( 1 , 12 )
After 97 iterations location = ( 1 , 11 )
After 98 iterations location = ( 0 , 11 )
After 99 iterations location = ( 0 , 12 )
After 100 iterations location = ( 0 , 11 )