Computer Science > QUESTIONS & ANSWERS > University of Illinois, Urbana Champaign CS CS 101 Generating a Random Walk A random walk in two (All)
Question Generating a Random Walk A random walk in two dimensions along a square grid has equal probability of veering off in any direction. Over time, this means that the expected total distance ... travel increases, but the mean expected position remains the origin (0,0) Compose a function random_walk_distance which accepts a number of steps n and an optional argument seed with default of 101. This function should return the distance traveled from the origin after a random walk of n steps of size 1, chosen from the four cardinal directions (in order: forward, backward, left, right). Your submission should include a function random_walk_distance( n,seed ). Note that this is just a random walk: there is no figure-of-merit f to track. Starter code def random_walk_distance( n,seed ): # Set the seed. xy = np.random.uniform( size=(2,) ) f = np.array( ( 0,-1 ) ) # "forward" looking at array in quadrant 4 b = np.array( ( 0,+1 ) ) # "back" looking at array in quadrant 4 l = np.array( ( -1,0 ) ) # "left" looking at array in quadrant 4 r = np.array( ( +1,0 ) ) # "right" looking at array in quadrant 4 for i in range( n ): # Take a random step, 25% chance in each direction. chance = np.random.uniform() if chance < 0.25: # Move forward. elif chance < _____: # Move backwards. elif chance < _____: # Move left. a: # Move right. # Split out x and y for convenience and return distance. return np.sqrt( x*x + y*y ) [Show More]
Last updated: 2 years ago
Preview 1 out of 3 pages
Buy this document to get the full access instantly
Instant Download Access after purchase
Buy NowInstant download
We Accept:
Can't find what you want? Try our AI powered Search
Connected school, study & course
About the document
Uploaded On
Apr 22, 2021
Number of pages
3
Written in
This document has been written for:
Uploaded
Apr 22, 2021
Downloads
0
Views
98
In Scholarfriends, a student can earn by offering help to other student. Students can help other students with materials by upploading their notes and earn money.
We're available through e-mail, Twitter, Facebook, and live chat.
FAQ
Questions? Leave a message!
Copyright © Scholarfriends · High quality services·