Return n points distributed over [xmin, xmax] x [ymin, ymax] according to (normalized) density distribution. with xmin, xmax = 0, density.shape[1] ymin, ymax = 0, density.shape[0] The algorithm here is a simple rejection sampling
时间: 2024-04-27 22:25:41 浏览: 79
# 根据密度分布返回分布在[xmin, xmax] x [ymin, ymax]上的n个点
# 其中xmin, xmax = 0, density.shape[1], ymin, ymax = 0, density.shape[0]
# 采用简单的拒绝抽样算法
def generate_points(density, n):
# 获取数组的行和列数
height, width = density.shape
# 归一化密度分布
density = normalize(density)
# 初始化points和count
points = np.zeros((n,2))
count = 0
# 采用拒绝抽样算法获取n个点
while count < n:
# 随机生成一个点的坐标
x, y = np.random.uniform(0, width), np.random.uniform(0, height)
# 判断该点是否被选中
if np.random.uniform(0,1) < density[int(y),int(x)]:
points[count,:] = (x,y)
count += 1
return points
阅读全文