PYTHON   27
marche au hasard
Guest on 14th March 2023 01:15:51 PM


  1. import random
  2. import numpy as np
  3. import matplotlib.pyplot as plt
  4.  
  5. xmin = -200
  6. xmax = 200
  7. ymin = 0
  8. ymax = 600
  9. x = np.linspace(xmin, xmax, xmax * 2)
  10. y = np.linspace(0, 0, xmax * 2)
  11. nb_pas = 50 # nombre de pas
  12. nb_part = 5000 # nombre de particules
  13. L = [-1, 1]
  14.  
  15. for k in range(nb_part) :
  16.     pos = 0
  17.     for tirage in range(nb_pas) :
  18.         deplacement = random.choice(L)
  19.         pos += deplacement
  20.     y[pos + xmax] += 1
  21.  
  22. plt.plot(x, y)
  23. plt.xlim(xmin, xmax)
  24. plt.ylim(ymin, ymax)
  25. plt.show()

Raw Paste

Login or Register to edit or fork this paste. It's free.