fork download
  1. from random import choice
  2. from matplotlib import pyplot, animation, rc
  3.  
  4. class random_walk_sim(object):
  5. def __init__(self, x, y):
  6. self.x = int(x)
  7. self.y = int(y)
  8. self.X = [x]
  9. self.Y = [y]
  10. self.init = (self.x, self.y)
  11. def random_move(self):
  12. self.x += choice([-1, 0, 1])
  13. self.y += choice([-1, 0, 1])
  14. self.X += [self.x]
  15. self.Y += [self.y]
  16. def show_history(self):
  17. pyplot.plot(self.X, self.Y)
  18. pyplot.show()
  19. def origin_count(self):
  20. return list(zip(self.X, self.Y)).count(self.init) - 1
  21.  
  22. if __name__ == '__main__':
  23. a = random_walk_sim(1, 1)
  24. [a.random_move() for i in range(1000)]
  25. a.show_history()
  26. [pyplot.scatter(m.X, m.Y, s = 0.5, color = "blue", alpha = 0.3) for m in [[j.random_move() for k in range(1000)] and j for j in [random_walk_sim(0, 0) for i in range(1000)]]]
  27. pyplot.show()
  28.  
Time limit exceeded #stdin #stdout 5s 94860KB
stdin
Standard input is empty
stdout
Standard output is empty