Python 3中随机游走的怪异结果? [英] Random walk's weird outcome in python 3?

查看:110
本文介绍了Python 3中随机游走的怪异结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习python,并在打印3维随机游走的新位置时遇到问题.没有弹出错误,但是很明显,正在打印的输出(x,y,z)是不合理的!当逐步模拟随机游走时,我假设每次(x,y,z)中仅应更改一个值.但这似乎不在输出中.我正在尝试对其进行调试,但仍然对确定真正的问题感到困惑.

I just start learning python and have a problem to print a new location of random walk in 3 dimensions. There is no error popping up, but it's obvious that the output (x, y, z) being printed is unreasonable! When simulating random walk step by step, I assume only one value in the (x, y, z) should be changed in each time. But it seems not in the output. I'm trying to debug it but still confused about identifying what the real problem is.

输出的标题行:

(0,-1,1)
(-1,0,1)
(-2,0,1)
(-2,1,2)
(-2,2,2)
(-2,2,3)
(-1,2,3)
(0,1,3)

我的动力:

此代码的目的是模拟3中随机游走的N步 方面.在每个步骤中,选择一个随机方向(北,南,东,西,上,下),并在该方向上采用大小为1的步骤.然后打印新位置.起始位置是原点(0、0、0).

The purpose of this code is to simulate N steps of a random walk in 3 dimensions. At each step, a random direction is chosen (north, south, east, west, up, down) and a step of size 1 is taken in that direction. The new location is then printed. The starting location is the origin (0, 0, 0).

我的代码:

import pdb
import random  # this helps us generate random numbers
N = 30  # number of steps
n = random.random()  # generate a random number

x = 0
y = 0
z = 0
count = 0 
while count <= N:
  if n < 1/6:
      x = x + 1           # move east
      n = random.random() # generate a new random number
  if n >= 1/6 and n < 2/6:
      y = y + 1           # move north
      n = random.random() # generate a new random number
  if n >= 2/6 and n < 3/6:
      z = z + 1           # move up
      n = random.random() # generate a new random number
  if n >= 3/6 and n < 4/6:
      x = x - 1           # move west
      n = random.random() # generate a new random number
  if n >= 4/6 and n < 5/6:
      y = y - 1           # move south
      n = random.random() # generate a new random number
  if n >= 5/6:
      z = z - 1           # move down
      n = random.random() # generate a new random number

  print("(%d,%d,%d)" % (x,y,z)) 
  count = count + 1
print("squared distance = %d" % (x*x + y*y + z*z))

环境:

Jupyter Notebook,Windows 10,Dell XPS 13中的Python 3.5

Python 3.5 in Jupyter Notebook, Windows 10, Dell XPS 13

推荐答案

您在每个if测试中将n设置为新的随机数:

You set n to a new random number in every if test:

if n < 1/6:
    x = x + 1           # move east
    n = random.random() # generate a new random number

这意味着 next if测试也可以在新的n上进行匹配,从而为每个步骤提供多个更改.

This means the next if test can then also match on the new n, giving you more than one change per step.

n = random.random()步骤移至循环的 top ,每个步骤仅生成一次.您可能还希望使用elif以避免进行过多的测试:

Move the n = random.random() step to the top of the loop, generating it only once per step. You probably want to use elif as well to avoid making too many tests:

N = 30  # number of steps

x = 0
y = 0
z = 0

for count in range(N):
    n = random.random() # generate a new random number
    if n < 1/6:
        x = x + 1           # move east
    elif n < 2/6:
        y = y + 1           # move north
    elif n < 3/6:
        z = z + 1           # move up
    elif n < 4/6:
        x = x - 1           # move west
    elif n < 5/6:
        y = y - 1           # move south
    else:
        z = z - 1           # move down

    print("(%d,%d,%d)" % (x,y,z)) 

我还改用了range()上的for循环,因此您不必手动递增和测试count.

I also switched to using a for loop over range() so you don't have to manually increment and test count.

这可以通过使用 list 存储方向,random.range()随机选择该列表中的索引,以及random.choice()选择更改方向的方向来进一步简化. :

This can be further simplified by using a list to store the directions, random.range() to pick an index in that list at random, and random.choice() to pick what direction to change the step in:

N = 30  # number of steps

pos = [0, 0, 0]

for count in range(N):
    index = random.randrange(3) # generate a new random index
    change = random.choice([-1, 1])
    pos[index] += change

    print(tuple(pos))

这篇关于Python 3中随机游走的怪异结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆