Skip to content

Instantly share code, notes, and snippets.

@jejones3141
Last active October 14, 2018 21:33
Show Gist options
  • Save jejones3141/9c2e691613dd5d423bddcc63dc3f49b1 to your computer and use it in GitHub Desktop.
Save jejones3141/9c2e691613dd5d423bddcc63dc3f49b1 to your computer and use it in GitHub Desktop.
quick hack for 10/12/18 Riddler Classic problem
import random, math
# WLOG, we take the field radius R to be 1 and the stake for the goat's rope
# to be at (1,0).
# random point in the field; sqrt() gives uniformly distributed distance from
# the origin (see https://programming.guide/random-point-within-circle.html;
# thanks, David)
def randPoint():
randr = math.sqrt(random.random())
theta = random.random() * 2 * math.pi
return randr * math.cos(theta), randr * math.sin(theta)
# generator of n random points in the field
def randSeq(n):
return (randPoint() for i in range(n))
# return True iff (x, y) is within reach of the goat with rope length r
def grazeable(x, y, r):
return (x - 1)**2 + y**2 <= r**2
# fraction of randomly selected points in field that are also grazeable
# should approximate the fraction of the field the goat can get to
def trial(n, r):
grazeables = sum(1 for x, y in randSeq(n) if grazeable(x, y, r))
return grazeables / n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment