diff --git a/frog-2.py b/frog-2.py index c0b0eec..6eca0f0 100644 --- a/frog-2.py +++ b/frog-2.py @@ -1,5 +1,7 @@ +import random + def frog(final_dist=100,max=6): - + # I wrote this quickly in a pub # Don't judge me @@ -7,8 +9,6 @@ def frog(final_dist=100,max=6): while total_dist <= final_dist: - import random - cap = 10**max trials = 0 @@ -33,6 +33,10 @@ def frog(final_dist=100,max=6): total_dist += 1 return "DONE" - + +def main(): + frog() - \ No newline at end of file +if __name__ == '__main__': + main() + diff --git a/frog-ph.py b/frog-ph.py new file mode 100644 index 0000000..ae6f73f --- /dev/null +++ b/frog-ph.py @@ -0,0 +1,30 @@ +# Paul Holt +# pcholt@gmail.com +# hereby committed to the PUBLIC DOMAIN + +def _crossing_hop_count(hops_remaining): + count = 0 + while hops_remaining > 0: + hops_remaining -= random.randrange(1,hops_remaining+1) + count += 1 + return count + +def crossing_histogram(pond_size, samples): + hist = [0 for i in xrange(pond_size+1)] + for i in xrange(samples): + hist[_crossing_hop_count(pond_size)] += 1 + return hist + +def average(samples=10**5, pond_size=3): + histogram = crossing_histogram(pond_size,samples) + total = 0 + for hop_count, occurrences in enumerate(histogram): + total += hop_count * occurrences + return (total/float(samples), histogram) + +def main(): + for pond_size in xrange(200): + print pond_size, average(pond_size=pond_size) + +if __name__ == '__main__': + main() \ No newline at end of file