Guest User

Untitled

a guest
Jan 18th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3.  
  4. def plot_random_walk(num_steps):
  5. """
  6. Plots a random walk of length num_steps. The initial
  7. position is y = 0.
  8.  
  9. Parameters
  10. ----------
  11. num_steps : int
  12. Number of steps to take in the random walk
  13. """
  14. y = np.zeros(num_steps)
  15.  
  16. for i in range(1, num_steps):
  17. # YOUR CODE GOES HERE
  18. # 1. Generate an integer n which can be 0 or 1 (use the function numpy.random.randint)
  19. # 2. if n is 0, set y[i] to y[i-1]+1
  20. # 3. if n is 1, set y[i] to y[i-1]-1
  21. # -------------------
  22. n = np.random.randint(0, 2)
  23. if n == 0:
  24. y[i] = y[i-1] + 1
  25. elif n == 1:
  26. y[i] = y[i-1] - 1
  27. # -------------------
  28. pass
  29. plt.plot(y)
  30.  
  31.  
  32. number_of_walks = 10
  33. number_of_steps = 100
  34.  
  35. for i in range(number_of_walks):
  36. plot_random_walk(number_of_steps)
Add Comment
Please, Sign In to add comment