Python Python Help for CSC131: Random Walk Simulation

  • Thread starter Thread starter Sthiel
  • Start date Start date
  • Tags Tags
    Python
AI Thread Summary
The discussion revolves around simulating a random walk problem using Python, where a "drunken man" moves left or right on a sidewalk represented by a list of 11 squares. The goal is to determine how long it takes for the man to exit the sidewalk and how many times he stands on each square. Participants share insights on setting up the program, emphasizing the importance of initializing a list to track the number of times the man occupies each square and using random number generation to dictate his movement. The simulation begins with the man at the center square, and each move updates his position and the corresponding count in the list. The code snippets provided illustrate the logic for movement, boundary checks, and output formatting. The conversation highlights the ease of implementing this simulation in Python compared to other languages, making it a useful exercise for understanding basic programming concepts.
Sthiel
Messages
5
Reaction score
0
"A classic problem that can be solved using a list is a random walk. Imagine a drunken man standing on the center square of a sidewalk consisting of 11 squares. At each step, the drunk can elect to go either left or right. How long will it be until he reaches the end of the sidewalk, and how many times will he have stood on each square? Write a Python program that solves this problem and simulates the steps taken by the drunk. The simulation ends when the drunken man moves out of the sidewalk. To help you with the solution, you can represent the sidewalk as a list. Store the number of times the drunk has stood on a square as a value in the list. The list can be created and initialized to 0's with the following statement:

times = [0] * 11
The function random.randint(0,1) outputs 0 or 1 with equal probability. Move to the left if 0 is produced, and to the right if 1 is produced. Maintain a variable that indicates the current location of the drunk and another variable that maintains the number of moves. Display the direction, the location, and the list at each step of the simulation. A sample output is as follows:

Starting at location 5
| 0 | 0 | 0 | 0 | 0 | 1 | 0 | 0 | 0 | 0 | 0 |
Moving right into location 6
| 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 | 0 | 0 | 0 |
Moving right into location 7
| 0 | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 0 | 0 | 0 |
Moving left into location 6
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 1 | 0 | 0 | 0 |
Moving right into location 7
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 0 | 0 | 0 |
Moving right into location 8
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 1 | 0 | 0 |
Moving right into location 9
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 1 | 1 | 0 |
Moving left into location 8
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 2 | 1 | 0 |
Moving right into location 9
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 2 | 2 | 0 |
Moving left into location 8
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 3 | 2 | 0 |
Moving right into location 9
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 3 | 3 | 0 |
Moving right into location 10
| 0 | 0 | 0 | 0 | 0 | 1 | 2 | 2 | 3 | 3 | 1 |
Moving right into location 11
number of moves is 11"

So, I've not had any problems understanding anything being taught in this class but I am having problems with how to implement things. This is an old lab assignment that I didnt finish. I'm not entirely sure how I would set this program up at all. I think if I could see how this is done I would have an easier time with the other homework assignment I have.

Any help would be appreciated.
 
Technology news on Phys.org
You should start by thinking of an algorithm. Really, the algorithm is in the paragraph, you just have to extract the important information (NOTE: I don't know much about Python, so I used C++; I would not, however, turn in the code I wrote below, as it was written in a few minutes):

There are 11 squares (array of 11 ints, but could be a size other than 11). Also, initial step count is 0. Initialize randomNumber and direction as well, since they will be used in a loop.

Code:
const int ARRAY_SIZE = 11;
int squareArray[ARRAY_SIZE];
int stepsCounter = 0;
int randomNumber;
string direction;

Starting location is 5. Set the corresponding array value to 1 (remember that the index of an array starts at 0). All others are set to 0.

Code:
int arrayLocation = 5;
squareArray[arrayLocation-1] = 1;

A random number of 0 or 1 is generated. If 1, move right; if 0, move left (ONLY move if arrayLocation is valid; if it is not, generate a random number again).

Code:
randomNumber = rand()%2;
if(randomNumber == 1)
{
  if((arrayLocation + 1) < (ARRAY_SIZE))
  {
	direction = "Right";
	arrayLocation++;
  }
  else
  {
	randomNumber = rand()%2;
  }
}
else
{
  if((arrayLocation - 1) >= 0)
  {
	direction = "Left";
	arrayLocation--;
  }
  else
  {
	randomNumber = rand()%2;
  }
}

For each valid step, increment the array value at arrayLocation by 1. Also increment stepsCounter by 1. Output the direction, arrayLocation, and squareArray.

Code:
stepsCounter++;
cout << "Moving " << direction << " into location " << arrayLocation+1 << endl;
for(int i = 0; i < ARRAY_SIZE; i++)
{
  cout << " " << squareArray[i];
}
cout << endl;

Repeat until you end on the last square squareArray[ARRAY_SIZE-1].

Code:
while(arrayLocation < (ARRAY_SIZE-1))
{
...
}

Once you reach the last square, display how many steps it took.

Code:
cout << "Number of moves is " << stepsCounter << endl;

Hope this helps!
 
Last edited:
Python can be much simpler than the above... let me put something here (when I figure out how to add code to the messages in this forum)
 
Last edited:
Here you go:

Works in Python 2.7

Code:
import random

times = [0] * 11
loc = 5
times[5] = 1
steps = 0

while 0 <= loc <= 10:
    step = random.randint(0,1)
    if step == 0:
        loc -= 1
        print 'Moving left to location', loc
    else:
        loc += 1
        print 'Moving right to location', loc
    times[loc] = times[loc] + 1
    steps += 1
    print times
print 'That took ', steps, ' steps.'
 
Slightly altered to cover the event where you step off the right (position 11) and the code produces and error (index out of range) or off the left and the code wraps around to the right in the list

Should work fine now.

Code:
import random

times = [0] * 11
loc = 5
times[5] = 1
track_steps = 0

while 0 <= loc <= 10:
    step = random.randint(0,1)
    track_steps += 1
    if step == 0:
        loc -= 1
        print 'Moving left to location', loc
        if loc < 0:
            break
    else:
        loc += 1
        print 'Moving right to location', loc
        if loc > 10:
            break
    times[loc] = times[loc] + 1
    print times
print 'That took ', track_steps, ' steps.'
 
Last edited:
Thank you. My next homework assignment is somewhat similar and that should definitely help me figure it out.
 
You're welcome. I'm new to this language myself but it seems to be a great language to learn programming with.

Good luck on your future assignments.
 

Similar threads

Replies
4
Views
1K
Replies
15
Views
2K
Replies
1
Views
1K
Replies
2
Views
1K
Replies
34
Views
3K
Replies
2
Views
2K
Back
Top