Created
February 7, 2021 07:31
-
-
Save nathwill/a4cb931997026e1b8b879615eab0698c to your computer and use it in GitHub Desktop.
example random walk routine
This file contains 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
#!/usr/bin/env ruby | |
# | |
class Rundganger | |
attr_accessor :coordinates, :path | |
def initialize | |
@coordinates = [0,0,0] | |
@path = [] << @coordinates.clone | |
end | |
def walk(steps = 3) | |
for _i in 1.upto(steps) | |
step() | |
@path << @coordinates.clone | |
end | |
end | |
def step | |
axis = rand(@coordinates.length) | |
direction = [1, -1].sample | |
@coordinates[axis] += direction | |
end | |
end | |
hans = Rundganger.new | |
hans.walk(10) | |
pp hans.path |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment