Skip to main content

With Shapely: https://shapely.readthedocs.io/en/latest/manual.html#polygons

The polygon in code below must be Polygon object.

import random
from shapely.geometry import Point

def generate_random(number, polygon):
    list_of_points = []
    minx, miny, maxx, maxy = polygon.bounds
    counter = 0
    while counter < number:
        pnt = Point(random.uniform(minx, maxx), random.uniform(miny, maxy))
        if polygon.contains(pnt):
            list_of_points.append(pnt)
            counter += 1
    return list_of_points
dmh126
  • 6.8k
  • 2
  • 22
  • 36