Last active
December 18, 2020 09:13
-
-
Save seandlg/a5a8b84d035dd6ef616548cdde1d66a2 to your computer and use it in GitHub Desktop.
Simple simulation of Spiegel Riddle (http://www.spiegel.de/wissenschaft/mensch/die-cleveren-gangster-raetsel-der-woche-a-1230429.html)
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
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