In case it helps someone, here is the code to implement Yves's answer:
- Draw points uniformly in the bounding box of the convex hull.
- Reject the points falling outside the hull.
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull
from matplotlib.patches import Rectangle
from matplotlib.path import Path
#Initial positions
pos = np.random.uniform(-30, 10, (20, 2))
#Convex hull of initial positions
hull = ConvexHull( pos )
#Bounding box
bbox = [hull.min_bound, hull.max_bound]
#Hull path
hull_path = Path( hull.points[hull.vertices] )
#Draw n random points inside the convex hull
n = 10**3
rand_points = np.empty((n, 2))
for i in range(n):
#Draw a random point in the bounding box of the convex hull
rand_points[i] = np.array([np.random.uniform(bbox[0][0], bbox[1][0]), np.random.uniform(bbox[0][1], bbox[1][1])])
#We check if the random point is inside the convex hull, otherwise we draw it again
while hull_path.contains_point(rand_points[i]) == False:
rand_points[i] = np.array([np.random.uniform(bbox[0][0], bbox[1][0]), np.random.uniform(bbox[0][1], bbox[1][1])])
#Plot
plt.scatter(pos[:, 0],pos[:, 1], marker='o', c='blue', alpha = 1, label ='Initial points')
for simplex in hull.simplices:
plt.plot(hull.points[simplex, 0], hull.points[simplex, 1], '-k')
plt.gca().add_patch(Rectangle((bbox[0][0], bbox[0][1]), bbox[1][0] - bbox[0][0], bbox[1][1] - bbox[0][1],facecolor = 'None', edgecolor = 'cyan'))
plt.scatter(rand_points[:, 0],rand_points[:, 1], marker='o', c='red', alpha = 0.31, label ='Random points inside hull')
plt.legend()
plt.savefig("fig.png", dpi = 300)
