Skip to content

Instantly share code, notes, and snippets.

@practicingruby
Created January 12, 2012 20:58
Show Gist options
  • Select an option

  • Save practicingruby/1603062 to your computer and use it in GitHub Desktop.

Select an option

Save practicingruby/1603062 to your computer and use it in GitHub Desktop.
class Node
Path = Struct.new(:probability, :destination)
def initialize(name)
@name = name
@paths = []
end
attr_reader :name
def path(probability, destination)
@paths << Path.new(probability, destination)
end
def next
@paths.flat_map { |e| [e.destination] * e.probability }.sample
end
end
a = Node.new("A")
e = Node.new("E")
a.path(4, e)
a.path(6, a)
e.path(3, e)
e.path(7, a)
30.times do
x = a.next
print x.name
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment