Reputation: 93
I am having a problem with my code mapping a random walk in 3D space. 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. Here is my code:
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))
The problem is I am getting more than a single step between each iteration. I've added comments showing the difference in steps between iterations.
Here are the first 10 lines of the output:
(0,-1,0) #1 step
(0,-2,0) #1 step
(1,-3,1) #4 steps
(1,-4,1) #1 step
(1,-3,1) #1 step
(1,-2,1) #1 step
(2,-2,0) #2 steps
(2,-2,0) #0 steps
(2,-2,0) #0 steps
(2,-1,0) #1 step
Upvotes: 1
Views: 1256
Reputation: 177
If you remove the multiple n = random.random()
from within the if statements and replace by a single n = random.random()
at start of the while loop then there will be only one step per loop.
Upvotes: 2
Reputation: 3089
This is because you are setting n
in each if-block
which is satisfying next if blocks as well.
Just take out n = random.random()
from all if blocks and place it just before end of while loop.
Tip:
To debug this I would've printed the value of n
in each if case.
Upvotes: 1
Reputation: 1608
Replacing multiple if
statements with chain of if-elif-else
solved the problem. Also, you could iterate using for
instead of while
. And in addition - you don't need to regenerate n
under each if
.
import random # this helps us generate random numbers
N = 30 # number of steps
x = 0
y = 0
z = 0
for _ in range(N):
n = random.random() # generate a new random number
if n < 1/6:
x += 1 # move east
elif 1/6 <= n < 2/6:
y += 1 # move north
elif 2/6 <= n < 3/6:
z += 1 # move up
elif 3/6 <= n < 4/6:
x -= 1 # move west
elif 4/6 <= n < 5/6:
y -= 1 # move south
else:
z -= 1 # move down
print("(%d,%d,%d)" % (x, y, z))
print("squared distance = %d" % (x*x + y*y + z*z))
Upvotes: 1
Reputation:
The issues lies with this line in each of your if statement : n = random.random(). Basically you should had used if and else if so if in a single iteration one if has been executed then it does not get executed again. Also you can add this line at the end of the all if statements instead of repeating it.n = random.random()
Upvotes: 1