Existing function for generating of random point in polygon in PostGIS may take a very long time in narrow and long polygons because the functions have to test lots of points that fall within bounding rectangle but outside actual polygon. Examples are here and here.
The idea is to use a function to subdivide bounding rectangle recursively and process only tiles that overlap with the polygon:
- subdivide the bounding rectangle into smaller rectangles (2, 4, or more, whichever is easier)
- randomly pick a rectangle
- create an intersection of the rectangle with the original polygon, return to the previous step if intersection is empty
- check if the ratio between the areas of the rectangle and intersection is big enough