elf on the shelf
elf on the shelf

Reputation: 19

How to use random numbers that executes a one dimensional random walk in python?

Start with a one dimensional space of length m, where m = 2 * n + 1. Take a step either to the left or to the right at random, with equal probability. Continue taking random steps until you go off one edge of the space, for which I'm using while 0 <= position < m.

We have to write a program that executes the random walk. We have to create a 1D space using size n = 5 and place the marker in the middle. Every step, move it either to the left or to the right using the random number generator. There should be an equal probability that it moves in either direction.

I have an idea for the plan but do not know how to write it in python:

Upvotes: 0

Views: 364

Answers (1)

Reblochon Masque
Reblochon Masque

Reputation: 36682

  • 1 - Start with a list initialized with 5 items (maybe None?)
  • 2 - place the walker at index 2
  • 3 - randomly chose a direction (-1 or + 1)
  • 4 - move the walker in the chosen direction
  • 5 - maybe print the space and mark the location of the walker
  • 6 - repeat at step 3 as many times as needed

Upvotes: 1

Related Questions