Created
September 6, 2009 03:31
-
-
Save davelyon/181631 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
""" | |
MIT License | |
---------------------------------------------------------------- | |
Copyright (c) 2009 David Lyon | |
Permission is hereby granted, free of charge, to any person | |
obtaining a copy of this software and associated documentation | |
files (the "Software"), to deal in the Software without | |
restriction, including without limitation the rights to use, | |
copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the | |
Software is furnished to do so, subject to the following | |
conditions: | |
The above copyright notice and this permission notice shall be | |
included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | |
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | |
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | |
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | |
OTHER DEALINGS IN THE SOFTWARE. | |
----------------------------------------------------------------- | |
About: | |
Simulates a drunk person walking on a pier up to a given limit for sizes. Prints out the size then the average time it took for the drunk to fall off of the pier. For more trials, change the 'trials' variable in drunkGuy. Defaults to 1000 trials. More is better, but takes MUCH longer. | |
""" | |
import random | |
def theTap(size): | |
""" theTap is a generator that returns points. It stops when x or y exceed the size, or hit -1.""" | |
loc = [int(size/2), int(size/2)] #where my drunk at? holds an 'x,y' pair | |
while( -1 < loc[0] < size and -1 < loc[1] < size): | |
loc = [x + random.randint(-1,1) for x in loc] | |
yield loc | |
def drunkGuy(piersize=20): | |
""" Takes a max pier size and tests 1 up to piersize to see how long on average a drunk guy will stay on the pier""" | |
trials = 1000 | |
for x in range(1, piersize+1): | |
time = 0 #holds the number of iterations, or 'time' | |
for y in range(0, trials): | |
points = [] # a list of points we've been to, a list of lists of points [x,y] | |
[points.append(p) for p in theTap(x)] #keep going back to the tap until you fall off the pier | |
time = time + len(points) | |
print("%02d" % (x), " ", "AVG", " ",time/trials) | |
drunkGuy(50) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment