[关闭]
@bintou 2017-11-01T22:46:31.000000Z 字数 1756 阅读 1646

PseudoRandomness vs RH

未分类


PseudoRandomness vs RH

  1. def rand_walk(n):
  2. step = 0
  3. for i in range(1, n):
  4. #temp = randint(0,2)
  5. temp = ZZ.random_element(0,3)
  6. if temp == 2:
  7. step += 1
  8. elif temp == 1:
  9. step -= 1
  10. # Output the result.
  11. print "Derivation from orgin", step
  12. print "The expectation from the orgin is:", floor(sqrt(n))
  13. #Define a rand_walk using RH
  14. def rand_walk_rh(n):
  15. step = 0
  16. for i in range(1, n + 1):
  17. fac = factor(i)
  18. length = len(fac)
  19. stay = false
  20. for j in range(length):
  21. if fac[j][1] > 1:
  22. print i, "don't move"
  23. stay = True
  24. break
  25. if not stay:
  26. if is_odd(length):
  27. step += 1
  28. print i, "move right"
  29. else:
  30. step -=1
  31. print i, "move left"
  32. # Output the result.
  33. print "Derivation from orgin", step
  34. print "The expectation from the orgin is:", floor(sqrt(n))

Python code

  1. #https://www.quora.com/How-do-I-solve-the-drunkards-walk-or-random-walk-simulation-using-Python-language
  2. import random
  3. def RandomWalk1(n):
  4. # n-length random walk on the number line
  5. return [1 if random.random() > 0.5 else -1 for i in xrange(n)]
  6. def RandomWalk2(n):
  7. # n-length random walk on the number line
  8. return [random.choice([-1, 1]) for i in xrange(n)]
  9. def RandomWalkFinalPosition(n):
  10. return sum(RandomWalk1(n))
  11. import numpy as np
  12. def RandomWalkNP(n):
  13. return np.random.choice([-1, 1], n)
  14. def RandomWalkGenerator1():
  15. while True:
  16. yield 1 if random.random() > 0.5 else -1
  17. def RandomWalkGenerator2(n=float('inf')):
  18. step = 0
  19. while step < n:
  20. step += 1
  21. yield 1 if random.random() > 0.5 else -1
  22. import random
  23. def Step(n_dim):
  24. move = [0 for i in xrange(n_dim)]
  25. dimension_to_move = random.randint(0, n_dim-1)
  26. direction = 1 if random.random() > 0.5 else -1
  27. move[dimension_to_move] = direction
  28. return move
  29. def RandomWalkMulti(n_steps, n_dim):
  30. return [Step(n_dim) for i in xrange(n_steps)]
  31. #https://plot.ly/python/random-walk/
  32. #http://mooc.ee/MTAT.03.236/2014_fall/uploads/Main/drunkard1.pdf
  33. #https://stackoverflow.com/questions/9421928/another-simple-random-walk-simulation-using-pythontwo-dimensional
  34. #https://stackoverflow.com/questions/26195815/python-drunkards-walk
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注