Skip to content

Instantly share code, notes, and snippets.

@seandlg
Last active December 18, 2020 09:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seandlg/a5a8b84d035dd6ef616548cdde1d66a2 to your computer and use it in GitHub Desktop.
Save seandlg/a5a8b84d035dd6ef616548cdde1d66a2 to your computer and use it in GitHub Desktop.
import random
# Set the following arguments
noSimulations = 100000
# The following graph is saved in the dictionary "graph"
#
# A--B--C--D
# | | | |
# E--F--G--H
# | | | |
# I--J--K--L
#
# A player moves from A to L by randomly choosing a path, moving only from top to bottom or left to right.
graph = {"a": ("b", "e"), "b": ("c", "f"), "c": ("d", "g"), "d": ("h"), "e": (
"f", "i"), "f": ("j", "g"), "g": ("h", "k"), "h": ("l"), "i": ("j"), "j": ("k"), "k": ("l")}
def moveThroughGraph():
startNode = "a"
currentNode = startNode
nodesVisited = []
nodesVisited.append(startNode)
for i in range(0, 5):
currentNode = random.choice(graph[currentNode])
nodesVisited.append(currentNode)
return nodesVisited
def getSimulations():
allSimulations = []
for i in range(noSimulations):
allSimulations.append(moveThroughGraph())
return allSimulations
def evaluateSimulations(simulationList):
count = 0
for simulation in simulationList:
if observerAt in simulation:
count += 1
return count
if __name__ == "__main__":
for observerAt in ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"]:
observerAtSpecifiedPointCount = evaluateSimulations(getSimulations())
print("The observer has been seen %d out of %d times at point %s. He was observerd with a probability of %s"
% (observerAtSpecifiedPointCount, noSimulations, observerAt, "{0:.0%}".format(float(observerAtSpecifiedPointCount)/float(noSimulations))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment