@bintou
2017-11-01T22:46:31.000000Z
字数 1756
阅读 1688
未分类
def rand_walk(n):
step = 0
for i in range(1, n):
#temp = randint(0,2)
temp = ZZ.random_element(0,3)
if temp == 2:
step += 1
elif temp == 1:
step -= 1
# Output the result.
print "Derivation from orgin", step
print "The expectation from the orgin is:", floor(sqrt(n))
#Define a rand_walk using RH
def rand_walk_rh(n):
step = 0
for i in range(1, n + 1):
fac = factor(i)
length = len(fac)
stay = false
for j in range(length):
if fac[j][1] > 1:
print i, "don't move"
stay = True
break
if not stay:
if is_odd(length):
step += 1
print i, "move right"
else:
step -=1
print i, "move left"
# Output the result.
print "Derivation from orgin", step
print "The expectation from the orgin is:", floor(sqrt(n))
#https://www.quora.com/How-do-I-solve-the-drunkards-walk-or-random-walk-simulation-using-Python-language
import random
def RandomWalk1(n):
# n-length random walk on the number line
return [1 if random.random() > 0.5 else -1 for i in xrange(n)]
def RandomWalk2(n):
# n-length random walk on the number line
return [random.choice([-1, 1]) for i in xrange(n)]
def RandomWalkFinalPosition(n):
return sum(RandomWalk1(n))
import numpy as np
def RandomWalkNP(n):
return np.random.choice([-1, 1], n)
def RandomWalkGenerator1():
while True:
yield 1 if random.random() > 0.5 else -1
def RandomWalkGenerator2(n=float('inf')):
step = 0
while step < n:
step += 1
yield 1 if random.random() > 0.5 else -1
import random
def Step(n_dim):
move = [0 for i in xrange(n_dim)]
dimension_to_move = random.randint(0, n_dim-1)
direction = 1 if random.random() > 0.5 else -1
move[dimension_to_move] = direction
return move
def RandomWalkMulti(n_steps, n_dim):
return [Step(n_dim) for i in xrange(n_steps)]
#https://plot.ly/python/random-walk/
#http://mooc.ee/MTAT.03.236/2014_fall/uploads/Main/drunkard1.pdf
#https://stackoverflow.com/questions/9421928/another-simple-random-walk-simulation-using-pythontwo-dimensional
#https://stackoverflow.com/questions/26195815/python-drunkards-walk