我有这个问题:
创建一个程序,该程序构建一 (1) 维和 100000 个站点的晶格。在这个晶格中,在随机位置放置了许多陷阱分子,它们的浓度为 c。将 1 个粒子放在格子上的随机位置,让它执行随机游走。在此步行中,您不会设置时间限制,即您不会声明特定的步数。当粒子落在陷阱上时,行走将停止............................注意边界条件。当粒子到达格子的边界时,它不应该被允许从格子中逃脱,而是留在格子中,要么回到原来的位置,要么被放置在格子的相反位置…… ..
我的方法显示在我创建的代码中(我在其中有评论)。
def steps1d(self,pos,c):
#pos: number of positions
#c: concentration of trap-particles
# array full of traps (zeros)
myzeros = sc.zeros(self.c*self.pos)
# grid full of available positions(ones)
grid = sc.ones(self.pos)
# distribute c*pos zeros(traps) in random positions (number of positions is pos)
traps = sc.random.permutation(pos)[:c*pos]
# the grid in which the particle is moving which has traps inside it
grid[traps] = myzeros
steps_count = [] # list which holds the number of steps
free = 0
for i in range(pos):
# the step of the particle can be 0 or 1
step=sc.random.random_integers(0,1)
for step in grid[:]:
if step == 1:
free += 1
steps_count.append(free)
else:
break
return steps_count
我有 3 个问题:
1)我以 pos=10 为例的结果如下:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 , 26, 27, 28, 29, 30, 31, 32, 33, 34, 35...]
我希望每次运行 10 个数字(变量 pos)。
2)我不确定如何处理边界条件。我在想类似的事情:
if free > grid.size:
free = free - 1
但我无法测试它。另外,我不确定这是否适用于网格的两个边界。
3)如果我想让第一步从网格中间开始,我该怎么做?
如果有人对此有提示,我将不胜感激。