Restart your subscription to keep accessing solutions. Your Chegg Study subscrip
ID: 3841456 • Letter: R
Question
Restart your subscription to keep accessing solutions. Your Chegg Study subscription will expire on May 24, 2017. CONTINUE MY SUBSCRIPTION home / study / engineering / computer science / questions and answers / can someone show me how to make a 2d random walk ... Your question has been answered Let us know if you got a helpful answer. Rate this answer Question: Can someone show me how to make a 2d random walk o... Bookmark can someone show me how to make a 2d random walk on python. I want to graph x vs t. the walk should be over a perod of 10000 steps each step per second. It should also randomize the dirsction of the theta between each step
Explanation / Answer
Below is the code , let me know in comments if you have queries
from random import randrange as rand
from numpy import cos, sin, radians
import numpy as np
import matplotlib.pyplot as plt
N = 100 # Size of square as a multiple of the step size.
NSteps = 10000 # Number of steps in simulation.
xStart = 0 # x coordinate of starting location. Origin is at middle of square
yStart = 0 # y coordinate of starting location. Origin is at middle of square
s = 1 # Step number.
x = xStart # x coordinate of point.
y = yStart # y coordinate of point.
xList = [] # List of the x coordinates of all points visited.
yList = [] # List of the y coordinates of all points visited.
def wrap(v, N):
if v > N/2:
return v - N, True
elif v < -N/2:
return v + N, True
return v, False
for j in range(NSteps):
angle = radians(rand(361))
x, wrap_flag_x = wrap(x + cos(angle), N)
y, wrap_flag_y = wrap(y + sin(angle), N)
if wrap_flag_x or wrap_flag_y:
xList.append(np.nan)
yList.append(np.nan)
xList.append(x)
yList.append(y)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.