Last active
December 17, 2015 06:29
-
-
Save austinogilvie/5565935 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var walk = function () { | |
| return {walk_it_out: walk_it_out} | |
| function randint () { | |
| var r = Math.random () | |
| return Math.round(r*10) | |
| }; | |
| function get_steps (n) { | |
| var steps = [], step; | |
| for (var i=0; i < n; i++) { | |
| step = Math.random () < 0.5 ? -1 : 1 | |
| steps.push(step * randint()) | |
| } | |
| return steps; | |
| }; | |
| function walk_it_out(n) { | |
| var start = 100 | |
| , current = start | |
| , steps = get_steps(n) | |
| , positions = [start]; | |
| for (var i=0; i< steps.length; i++) { | |
| current += steps[i] | |
| positions.push(current) | |
| } | |
| return positions | |
| } | |
| }() | |
| walk.walk_it_out(1000) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment