Question
I need help modifying the code below using tkinter and be able to run the code in IDLE 3.7 Thank you. Below is the question:
I need help modifying the code below using tkinter and be able to run the code in IDLE 3.7 Thank you.
Below is the question:
A random walk is a particular kind of probabilistic (pseudo-random) simulation that models certain statistical systems, such as Brownian motion of particles or molecules. Coin flipping is an example of a one-dimensional random walk--one dimensional because you only can go forward (when you flip heads) or backward (when you flip tails) along a straight line. Suppose you take a random walk of n steps. How many steps away from your starting point would you expect to end up on average, if you repeated the experiment many times?
Now suppose we make it two-dimensional. Suppose you can go forward, backward, left and right. How many steps away from your starting point would you expect to end up on average, if you repeated this experiment many times?
And finally, suppose instead of just four choices, we could go any direction? We can generate a random direction using an angle from the x axis using equation 1. Then we can use equations 2 and 3 to generate new positions for each time step.
(1) angle = random() * 2 * math.pi (2) x = x + cos(angle) (3) y = y + sin(angle)
Your program should take the number of steps as input. Assume the walker always starts at 0,0 in a 100x100 unit grid. Create a graphical program that will draw a line to trace the path of the walk as it progresses. At the end, print out the straight-line distance and actual distance traveled to the console or GUI, rounded to the nearest whole unit.
Here's the copy code:
import math import random import turtle
#function to calculate the distance between 2 points def distance(x1, y1, x2, y2): dx = x1 - x2; dy = y1 - y2; dist = math.sqrt(dx * dx + dy * dy); return dist
def main():
#prompt and get the number of steps steps = int(input('How many steps? ')) #initialize x1 = 0 y1 = 0 actualDistance = 0 s = turtle.Screen() t = turtle.Turtle() t.color('red')
#iterate for no.of steps specified for k in range(steps): #use formula to get next values angle = random.random() * 2 * math.pi x2 = x1 + math.cos(angle) y2 = y1 + math.sin(angle) #print('(%d, %d)' % (x2, y2)) actualDistance = actualDistance + distance(x1, y1, x2, y2) t.left(angle) #turn the tutle t.goto(x2, y2) #goto new location x1 = x2 y1 = y2
#calcuate the straightline distance between origin and final position straightDistance = distance(0, 0, x1, y1) print('Actual distance is %.2f and straight line distance is %.2f' % (actualDistance, straightDistance)) s.exitonclick() if __name__ == "__main__": main()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started