HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Random walk in Python + turtle

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
randomwalkpythonturtle

Problem

I decided to implement a random walk.

The rules

The rules are easy, an object is moved in the direction dictated by random or pseudo-random numbers.
If you want to read more about it, see the Wikipedia Page.

A simple example

Let's say we decide:

  • 1 = "Go right"



  • 2 = "Go down"



  • 3 = "Go left"



  • 4 = "Go up"



We then generate the random or pseudo-random sequence: 3,1,4,1.
And we convert it to directions:

  • Left



  • Right



  • Up



  • Right



we obtain the following:

A more complex example

Runnning my code with:

  • step_size = 15



  • step_number = 1000



Generated the following walk:

Running the code with the exact same parameters may yield completely different results because pseudo-random numbers are used.

I think my code is well written and fully Pep8 compliant, two things bother me

-

-
I define many go_ methods to go to absolute directions, I could have done something like:

turn_of_random_deegres()
turtle.forward(step)

But it looked cleaner to me, albeit a little longer, to define absolute moving.

-

  • The go_ methods change between logo and standard edition. (Control-Find setheading in this doc-page)



The code

import turtle
import random

def go_right(step):
     turtle.setheading(0)
     turtle.forward(step)

def go_up(step):
     turtle.setheading(90)
     turtle.forward(step)

def go_left(step):
     turtle.setheading(180)
     turtle.forward(step)

def go_down(step):
     turtle.setheading(270)
     turtle.forward(step)

def make_random_walk(step_size, step_number):
    move_dict = {1: go_up,
                 2: go_right,
                 3: go_left,
                 4: go_down
                 }
    for _ in range(step_number):
        move_in_a_direction = move_dict[random.randint(1, 4)]
        move_in_a_direction(step_size)

if __name__ == "__main__":
    turtle.hideturtle()
    turtle.speed("fastest")
    make_random_walk(15, 1000)

Solution

Your code looks nice and seems to be fully PEP 8 compliant indeed. The fact that you have 1 blank line between functions except in one place where you have 2 puzzles me a bit but that's not a huge issue.

You can actually make your code much easier. Here are a individual steps, you'll find the final code at the end.

-
Notice that you call turtle.forward(step) in each go_somewhere function. You might as well remove this and put it once and for all after your call to move_in_a_direction(step_size). Now, the go_somewhere function doesn't need to be given a step anymore.

-
Notice that the different function go_somewhere are just a call to setheading with a custom parameter. You could transform move_dict to map numbers to angles and call turtle.setheading(move_dict[random.randint(1, 4)).

-
Notice that your map is just converting 1 into 0, 2 into 90, 3 into 180, 4 into 270. This can be substitued with a simple operation : remove 1 then multiply by 90. You now have turtle.setheading((random.randint(1, 4) - 1) * 90).

-
Notice that generating an integer in [0, 1, 2, 3] makes things even easier as you don't need to remove 1.

Here's the corresponding code :

def make_random_walk(step_size, step_number):
    for _ in range(step_number):
        turtle.setheading(90 * random.randint(0, 3))
        turtle.forward(step_size)

Code Snippets

def make_random_walk(step_size, step_number):
    for _ in range(step_number):
        turtle.setheading(90 * random.randint(0, 3))
        turtle.forward(step_size)

Context

StackExchange Code Review Q#74495, answer score: 7

Revisions (0)

No revisions yet.