0

I'm new to this, but I'm trying to create a program that goes on a Random Walk in turtle and I have no idea what I'm doing.This is the program that I need to create Requirements, and this is what I have so far Code. I'm starting to get a little lost, and am wondering if I 'm headed towards the right direction or if i need to scrap it and start all over again

Thanks,

import turtle
import random
import math

def start():
    myS = turtle.Screen()
    myS.title("Random Walk")
    border = 50
    myWin.setworldcoordinates(-border, -border, border, border)

def go(heading, step_size):
    turtle.setheading(heading)
    turtle.forward(step_size)

def random_walk(step_size, steps):
    angle = random.random() * 2 * math.pi
    x = 0
    y = 0
    x = x + math.cos(angle)
    y = y + math.sin(angle)
    coord = (x, y)
    for _ in range(steps):
        go(random.choice(coord), step_size)

if __name__ == '__main__':
    turtle.hideturtle()
    turtle.speed('fastest')
    random_walk(15, 1000)
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Kalosak
  • 13
  • 2
  • 2
    Please do not include code or text in an image. You've already typed the code, but please type in the requirements as well. – FatihAkici Nov 03 '18 at 19:37
  • 1
    Oh sorry about that, i'm new to this and was trying to figure it out. I'll try and include it in the text instead of an image next time – Kalosak Nov 03 '18 at 21:45

2 Answers2

1

Your random_walk function does way to many things; I think it grew over your head a little. What it should do for every step:

  • Calculate a random angle between 0 and 2*math.pi
  • Calculate x and y with the given formulas
  • Call turtle.goto(x,y) to go to these coordinates

Also there was a little typo in start.

import turtle
import random
import math

def start():
    myS = turtle.Screen()
    myS.title("Random Walk")
    border = 50
    myS.setworldcoordinates(-border, -border, border, border)

def go(heading, step_size):
    turtle.setheading(heading)
    turtle.forward(step_size)

def random_walk(steps):
    x = 0
    y = 0
    for i in range(steps):
        angle = random.random()*2*math.pi
        x = x + math.cos(angle)
        y = y + math.sin(angle)
        turtle.goto(x,y)



if __name__=="__main__":
    start()
    random_walk(100)
user8408080
  • 2,428
  • 1
  • 10
  • 19
  • An angle is just a number. This number gets chosen randomly with `random.random()*2*math.pi`. With this number you calculate a set of coordinates `(x,y)`. Then you go to these coordinates. In every loop there will be a new angle randomly calculated. Hope this is clearer now? – user8408080 Nov 03 '18 at 19:18
  • Thanks! yeah I changed it from myWin to myS and didn't catch that. Also,when you changed the random walk to 100, is that because the grid is 100 x 100? or can i choose any number as well as 100? – Kalosak Nov 03 '18 at 19:20
  • The number `100` given to `random_walk` is the *number of steps* that shall be calculated, it has nothing to do with the window size – user8408080 Nov 03 '18 at 19:22
  • Okay, and how do I draw the line from the end of that and finish at the start? I know the start is at 0,0, so I can have a line draw from the end and set the position to 0,0 but I don't know how to find the end of that drawing? (Let alone the actual distance and the straight line distance) – Kalosak Nov 03 '18 at 19:31
  • The turtle is already at the end, so you can just call `turtle.goto(0,0)` – user8408080 Nov 03 '18 at 19:33
  • Oh wait, i just did turtle.setpos(0,0) and it worked, but yeah how do I find the distance of the walk and the straight line? – Kalosak Nov 03 '18 at 19:34
  • You mean the distance from the last position of the turtle to (0,0)? – user8408080 Nov 03 '18 at 19:37
  • Yeah, I'm trying to figure out the straight-line distance and actual distance traveled to the console, rounded to the nearest whole unit – Kalosak Nov 03 '18 at 19:38
  • I know that i could use the Distance formula which is the square root of (x2-x1)^2 + (y2-y1)^2 but i don't know how i'm supposed to know the point where it ends – Kalosak Nov 03 '18 at 19:39
  • And that's for the straight line, i'm not quite sure how to do the actual distance traveled – Kalosak Nov 03 '18 at 19:40
  • I'm pretty sure, there is something like `turtle.getpos()`. You have to do at least some research – user8408080 Nov 03 '18 at 19:40
  • You would have to calculate and sum up the length if each segment obviously – user8408080 Nov 03 '18 at 19:43
0

Because this looks like a homework assignment I don't want to just fix your errors but instead give you pointers to the right solution. I will also hide hints behind spoilers...

  1. if you run random_walk() with e.g. parameters random_walk(1, 1) your will notice that the direction of the walk will not appear random. Use some form of debugging to find out why that is

    your go() functions expects an angle, look at what random.choice() produces

  2. if you fix the issue number 1. you will see that the turtle will not draw a random line at all again

    looks like the angle does not change every step. find a solution that updates the angle before each step!

  3. units of angles: find out what unit setheading() expects: hint

  4. your requirements mention the output of straight line distance and distance travelled. This somehow sounds like you need some form of tally variable (one or several). hint: if you read the turtle documentation carefully you might find a funtion that maybe makes this task easier

    read the documentation for turtle.position().

0x6d64
  • 153
  • 2
  • 9
  • So how to I make it an angle? The reason i had random.choice was to choose between the x and the y because i thought that would make it random but i think that's where i started to get really confused. I made x=0 and y = 0 to define them but should I have done something else? – Kalosak Nov 03 '18 at 19:16
  • @Kalosak: don't get confused about the types of coordinates. goto() uses x and y values, setheading() (which I find more useful for your task) expects an angle between 0 and 360. So you need to generate a random angle in that range. My advice is to get rid of x and y for now and create code that generates a random angle and draws a line in that angle. If this works properly expand to generating a bunch of those steps that draw randomly like a cloud around the starting point. You can finally think about getting the distances right (read number 4 in my answer for hints). – 0x6d64 Nov 03 '18 at 20:02