Python Help for CSC131: Random Walk Simulation

  • Thread starter Sthiel
  • Start date
  • Tags
    Python
In summary: The function random.randint(0,1) outputs 0 or 1 with equal probability. This function moves the drunk left or right, depending on whether the random number is 0 or 1. The variables times and loc are updated to show the drunk's current position and the number of moves made, respectively.
  • #1
Sthiel
6
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
  • #2
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:
  • #3
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:
  • #4
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.'
 
  • #5
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:
  • #6
Thank you. My next homework assignment is somewhat similar and that should definitely help me figure it out.
 
  • #7
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.
 

Related to Python Help for CSC131: Random Walk Simulation

1. What is a random walk simulation?

A random walk simulation is a computational model used in various scientific fields to simulate the random movement of particles or individuals in a given space. It involves generating a series of random steps or movements and tracking the resulting path or trajectory.

2. Why is Python used for random walk simulations in CSC131?

Python is a popular programming language for scientific computing due to its readability, versatility, and vast library of specialized tools and packages. It also has efficient data structures and supports object-oriented programming, making it suitable for complex simulations like random walks.

3. How do I generate random numbers in Python for a random walk simulation?

To generate random numbers in Python, you can use the random module, which provides various functions for generating random integers, floating-point numbers, and sequences. For a random walk simulation, you can use the random.choice function to randomly select a step or direction from a list of possible options.

4. Can I visualize the results of a random walk simulation in Python?

Yes, Python has several libraries, such as matplotlib and seaborn, that can be used to create visualizations of data and simulations. You can plot the resulting path or trajectory of the random walk using these libraries to better understand the behavior of the simulation.

5. Are there any limitations to random walk simulations in Python?

Random walk simulations in Python are limited by the computational power and memory of the system being used. As the number of steps or particles in the simulation increases, so does the processing time and memory usage. Additionally, the accuracy of the simulation may be affected by the quality of the random number generator used.

Similar threads

  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
2
Views
924
Replies
1
Views
2K
  • Programming and Computer Science
Replies
34
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
3
Views
970
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Back
Top