Random walk's weird outcome in python 3?
<p>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.</p>

<p>The output's head lines:</p>

<pre><code>(0,-1,1)
(-1,0,1)
(-2,0,1)
(-2,1,2)
(-2,2,2)
(-2,2,3)
(-1,2,3)
(0,1,3)
</code></pre>

<p>My motivation:</p>

<blockquote>
  <p>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). </p>
</blockquote>

<p>My code:</p>

<pre><code>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 &lt;= N:
  if n &lt; 1/6:
      x = x + 1           # move east
      n = random.random() # generate a new random number
  if n &gt;= 1/6 and n &lt; 2/6:
      y = y + 1           # move north
      n = random.random() # generate a new random number
  if n &gt;= 2/6 and n &lt; 3/6:
      z = z + 1           # move up
      n = random.random() # generate a new random number
  if n &gt;= 3/6 and n &lt; 4/6:
      x = x - 1           # move west
      n = random.random() # generate a new random number
  if n &gt;= 4/6 and n &lt; 5/6:
      y = y - 1           # move south
      n = random.random() # generate a new random number
  if n &gt;= 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))
</code></pre>

<p>Environment:</p>

<p>Python 3.5 in Jupyter Notebook, Windows 10, Dell XPS 13</p>

<p>You set <code>n</code> to a new random number in <em>every <code>if</code> test</em>:</p>

<pre><code>if n &lt; 1/6:
    x = x + 1           # move east
    n = random.random() # generate a new random number
</code></pre>

<p>This means the <em>next</em> <code>if</code> test can then also match on the new <code>n</code>, giving you more than one change per step.</p>

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

<pre><code>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 &lt; 1/6:
        x = x + 1           # move east
    elif n &lt; 2/6:
        y = y + 1           # move north
    elif n &lt; 3/6:
        z = z + 1           # move up
    elif n &lt; 4/6:
        x = x - 1           # move west
    elif n &lt; 5/6:
        y = y - 1           # move south
    else:
        z = z - 1           # move down

    print("(%d,%d,%d)" % (x,y,z)) 
</code></pre>

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

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

<pre><code>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))
</code></pre>

python random