Python Random Walk Simulation – How to set the particle to be released from the edge of a box

Question:

Here is some python code for a random walk simulation. The code allows you to set the particle’s initial position however I want the particle to be released specifically from the edge of a "box"/the array, before it does its random walk.

import numpy as np
import random
import matplotlib.pyplot as plt

np.random.seed(25)

x_init = 0
y_init = 0

x = [x_init]
y = [y_init]

n = 1000

x_array = np.random.randint(-1,2,n)
y_array = np.random.randint(-1,2,n)

for i in range(n):

    # Update position
    x_init += x_array[i]
    y_init += y_array[i]

    # Append new position
    x.append(x_init)
    y.append(y_init)

plt.plot(x,y)
plt.show()

I have tried manually setting the axis however inevitably this just reproduces the same random walk and cuts off anything not in the barriers of the axis – rather than creating the random walk from that point.

import numpy as np
import random
import matplotlib.pyplot as plt

np.random.seed(25)

x_init = 0
y_init = 0

x = [x_init]
y = [y_init]

n = 1000

x_array = np.random.randint(-1,2,n)
y_array = np.random.randint(-1,2,n)

for i in range(n):

    # Update position
    x_init += x_array[i]
    y_init += y_array[i]

    # Append new position
    x.append(x_init)
    y.append(y_init)

plt.axis([0,10,0,10]);
plt.plot(x,y)
plt.show()

How would I fix the code so that i can set the initial position to be from the edge of the box/array?

Asked By: Ellie

||

Answers:

Your comment clarified that you want to contain the particle in its random walk inside a pre-defined box.

Below code does that, but please take into account this is just one way of solving this issue:

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(25)

x_init = 0
y_init = 0

# Define your box here by setting an x- and y-range
x_range = (-100, 100)
y_range = (-100, 100)

x = [x_init]
y = [y_init]

# Reduced number of points to better show the contained walking path
n = 100

# Ask the random number generator to stay within the bounds of the box
# by setting the low and high values equal to the low and high values
# of the range
x_array = np.random.randint(low=x_range[0],high=x_range[1],size=n)
y_array = np.random.randint(low=y_range[0],high=y_range[1],size=n)

for i in range(n):

    # Update position
    # Check if the new position exceeds the set range, if so change the sign of sum
    # This makes sure the point stays within the bounding box
    x_new = x_init + x_array[i]
    if (x_new <= x_range[0] or x_new >= x_range[1]):
        x_new = x_init - x_array[i]

    # Similarly for the y position
    y_new = y_init + y_array[i]
    if (y_new <= y_range[0] or y_new >= y_range[1]):
        y_new = y_init - y_array[i]

    # Append new position
    x.append(x_new)
    y.append(y_new)

# No need to set plot axes explicitly as you are already containing the
# walking point inside the bounds, and the auto-axes will do just fine
plt.plot(x,y)
plt.show()

enter image description here

Answered By: Saaru Lindestøkke
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.