Skip to content

Instantly share code, notes, and snippets.

@anddam
Last active October 30, 2016 19:08
Show Gist options
  • Select an option

  • Save anddam/4852f03274680c33496203f8a670f56e to your computer and use it in GitHub Desktop.

Select an option

Save anddam/4852f03274680c33496203f8a670f56e to your computer and use it in GitHub Desktop.
from random import random
from math import sqrt
"""Generate coordinates for set of two random points in the [0, 1] square plane"""
def four_randoms():
return (random() for __ in range(4))
# Use yield to create an iterator
def coordinates(limit=10):
for _ in range(limit):
yield four_randoms()
# Return a generator expression
def coordinate_factory(limit=10):
return (four_randoms() for __ in range(limit))
def distance(x1, y1, x2, y2):
# debug print
# print(x1, y1, x2, y2)
return sqrt((x2-x1)**2 + (y2-y1)**2)
set_one = [distance(*c) for c in coordinates()]
set_two = [distance(*c) for c in coordinate_factory()]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment