0

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?

Ellie
  • 5
  • 2
  • Can you further explain what your problem is? When I run the code, I see exactly what I would expect. – Marcello Zago Feb 20 '23 at 13:28
  • @MarcelloZago Hiya, thank you for having a look. Essentially I am trying to run a code so that a particle starts a random walk from the edge of a box (and is then contained in the box when it completes its random walk). However I have only managed to get the particle to be released from a random initial position (which can be set manually within the code) however this then adjusts the axis of the box to fit the random walk and places the particle in the middle. - Sorry if that doesn't make any more sense! – Ellie Feb 20 '23 at 13:37

1 Answers1

1

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

Saaru Lindestøkke
  • 2,067
  • 1
  • 25
  • 51