Skip to content

Instantly share code, notes, and snippets.

@cwillmor
Last active September 17, 2026 17:21
Show Gist options
  • Select an option

  • Save cwillmor/26b5ff1d2a92d891ddcfcc4b1348c486 to your computer and use it in GitHub Desktop.

Select an option

Save cwillmor/26b5ff1d2a92d891ddcfcc4b1348c486 to your computer and use it in GitHub Desktop.
chord intersection problem
import math
import random
def random_angle():
"""Return a uniformly random angle on the unit circle."""
return random.random() * 2 * math.pi
def chords_cross(a1, a2, b1, b2):
"""
Return True if the chord from angle a1 to angle a2 crosses the chord from
angle b1 to angle b2.
"""
a1, a2 = sorted((a1, a2))
b1, b2 = sorted((b1, b2))
if a1 < b1:
return a1 < b1 < a2 < b2
else:
return b1 < a1 < b2 < a2
def trial():
a1, a2, b1, b2, c1, c2 = [random_angle() for i in range(6)]
return (chords_cross(a1, a2, b1, b2) and
chords_cross(a1, a2, c1, c2) and
chords_cross(b1, b2, c1, c2))
trial_count = 100000
success_count = sum(trial() for i in range(trial_count))
print(f"experimental value after {trial_count} trials: {success_count / trial_count}")
print(f"theoretical value: {1/15}")
"""
$ python3 chords.py
experimental value after 100000 trials: 0.06687
theoretical value: 0.06666666666666667
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment