使用 random 模块生成一维随机游走
我试图仅使用 random 模块生成一维随机游走。如果某个时刻的位置是x
,那么下一个位置可以等概率是x+1
或x-1
。我需要在 100 次移动后找到最终位置 (start=0
)。
我开发了以下代码,但我不确定应该如何定义选择之间的相等概率。
import random
def randomwalk1D(n):
x = 0
start = x
xposition = [start]
probabilities = [0.5, 0.5]
for i in range(1, n + 1):
step = random.choice(probabilities)
if step > probabilities[0]:
x += 1
elif step < probabilities[1]:
x -= 1
xposition.append(start)
return xposition
该函数仅返回零作为结果(输入n = 100
)。我只想使用 random
模块。有人可以建议从这里做什么吗?
I was trying to generate a random walk in 1D just with the random
module. If a position at a certain moment is x
, then the next position can be x+1
or x-1
with equal probability. I need to find the final position after 100 moves (start=0
).
I have developed the following code, but I am not sure how I should should define the equal probability among the choices.
import random
def randomwalk1D(n):
x = 0
start = x
xposition = [start]
probabilities = [0.5, 0.5]
for i in range(1, n + 1):
step = random.choice(probabilities)
if step > probabilities[0]:
x += 1
elif step < probabilities[1]:
x -= 1
xposition.append(start)
return xposition
The function return just zeroes as result (putting n = 100
). I only want to only use the random
module. Could someone advise on what to do from here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您的代码有两个问题:
-1
和1
的选择传递给random.choice()
,而不是权重这些选择。您还可以摆脱与概率和采样步骤中的元素的直接比较。如果您想要加权采样,请使用numpy.random.choice
。start
添加到xpositions
列表中,即使您从未更新它。要解决此问题,请改为附加x
,它表示实际的当前位置。这是解决这两个问题的代码片段。
There are two issues with your code:
-1
and1
torandom.choice()
, not the weights of those choices. You can also get rid of the direct comparisons with the elements fromprobabilities
and the sampled step. If you want weighted sampling, usenumpy.random.choice
.start
to thexpositions
list repeatedly, even though you never update it. To resolve, appendx
instead, which represents the actual current position.Here is a code snippet that resolves both issues.
已经发布了一些很好的答案,所以我会补充说可以进行一些优化:
这会删除
start
,因为我们可以简单地定义x = 0
并且因为您如果希望您的列表以此值开头,请实例化包含[x]
的列表。我们使用的是
for _ in range(n)
,而不是for i in range(1, n + 1):
。这会循环相同的次数,但从默认值 0 开始。因为没有使用变量i
,所以我们使用_
,这是一个标准,表示该值并不重要,我们只想循环某个数字次。我们使用
random.randint(0,1)
,而不是数字列表和random.randchoice
。这个范围是包含在内的,所以它将返回 0 或 1。然后,因为
1
是“true”,我们可以简单地测试if step:
来测试是否为 1,并且否则使用else:
。编辑:
如果需要加权数字,可以对代码进行以下修改:
step
将被定义为 0 或 1,其中 0 的可能性是 1 的 10 倍。There's some good answers already posted, so I'll add that there's some optimizations that could be made:
This removes
start
, as we can simply definex = 0
and since you want your list to start with this value, instantiate the list with[x]
included.Instead of
for i in range(1, n + 1):
, we're usingfor _ in range(n)
. This cycles the same amount of times, but starting at the default of 0 instead. Because there's no use of the variablei
, we're using_
which is a standard to indicate that the value doesn't matter, we only want to cycle a certain number of times.Instead of a list of numbers and
random.randchoice
, we're usingrandom.randint(0,1)
. This range is inclusive, so it will return 0, or 1.Then, because
1
is "truthy", we can simply testif step:
to test if 1, and then useelse:
otherwise.Edit:
If weighted numbers are needed, the following modification could be made to the code:
step
will be defined as either 0 or 1, with 0 being 10 times more likely than 1.我认为您的代码中有两个单独的问题,导致您看到的结果:
random.choice(probabilities)
表达式的结果始终为 0.5。这样,下面的两个 if 语句就永远不会被触发,并且x
始终保持相同的值。此外,对于 random.choice 的实际用途可能会有些混乱。
它需要一个可迭代对象,并统一选择其中一项进行输出。由于您给了它
[0.5, 0.5]
这意味着您只能得到 0.5 作为输出。也许您可以使用random.uniform(0, 1)
作为step
来生成一个统一的随机数。这样你就可以根据step >= 0.5
或step <= 0.5 来决定行走的方向。 0.5。
I think there are two separate problems in your code, that are leading to the results you are seeing:
random.choice(probabilities)
expression is always a value of 0.5. With that, both of the following if statements can never be triggered, andx
always remains the same value.xposition.append
you are appending the start value, which also remains the same over iterations of the for loop, so only 0 will ever be appended.Additionally, there might be some confusion about what random.choice actually does.
It takes a iterable, and uniformly chooses one of it's items to output. Since you have given it
[0.5, 0.5]
that means, you can only ever get 0.5 as an output. Perhaps you could just generate a uniformly random number, usingrandom.uniform(0, 1)
asstep
. That way you can just decide the direction to walk in based onstep >= 0.5
orstep < 0.5
.