如何对空间相关的随机数分布进行编程



我写了一个例程,在我的研究区域随机(均匀(分布任意直径的圆。

def no_nearby_dots(new_dot, dots_sim, min_distance):
for dot in dots_sim:
if np.sqrt((dot[0] - new_dot[0]) ** 2 + (dot[1] - new_dot[1]) ** 2) <= min_distance:
return False
return True
while realizations < simulations:
dots_sim = []
new_dot = True
dots_sim.append((np.random.uniform(xmin, xmax), np.random.uniform(ymin, ymax)))
failed_attempts = 0
while new_dot:
xp = np.random.uniform(xmin, xmax)
yp = np.random.uniform(ymin, ymax)
if no_nearby_dots((xp, yp), dots_sim, diameter):
dots_sim.append((xp, yp))
failed_attempts = 0
else:
failed_attempts += 1
if len(dots_sim) == n_sim:
new_dot = False
if failed_attempts > 2000:
new_dot = False
print('ERROR...exit loop')
break
x_sim = [dot[0] for dot in dots_sim]
y_sim = [dot[1] for dot in dots_sim]

我想在初始点周围引入第二个圆,其中分布点的可能性朝着内边界呈指数级减小->我想防止;硬";边界,允许点出现在平面上的任何地方,但不靠近diameter,此外,它们只能出现在diameterdiameter2之间的一定程度上。

有什么办法吗?

这里有一个想法。

diameter/2diameter2/2之间选择一个随机半径,然后在该半径形成的圆中生成一个随机点。有很多方法可以选择符合您要求的半径。例如,下面选择一个半径,使非常接近diameter2/2的半径更有可能被选择:

radius = (diameter1/2) + ((diameter2/2) - (diameter1/2)) * random.random()**(1/20)

注意,1/20是均匀(0,1(随机数的第20个根。尝试将1/20更改为其他值,看看会发生什么。

还有其他方法可以通过这种方式选择半径,它们都可以用概率密度函数来描述(有关更多信息,请参阅以下答案:在圆内(均匀地(生成一个随机点,这显示了线性密度函数如何导致圆内点的均匀分布(。

我解决了它,这就是我所做的:

while realizations < simulations:
dots_sim = []
new_dot = True
dots_sim.append((np.random.uniform(x_min, x_max), np.random.uniform(y_min, y_max)))
failed_attempts = 0
while new_dot:
x = np.random.uniform(x_min, x_max)
y = np.random.uniform(y_min, y_max)
diameter_simulation = np.random.uniform(min_diameter, max_diameter)
if no_nearby_dots((x, y), dots_sim, diameter_simulation):
dots_sim.append((x, y))
failed_attempts = 0
else:
failed_attempts += 1
if len(dots_sim) == len(x_coordinate):
new_dot = False
if failed_attempts > 1000:
new_dot = False
print('ERROR... -> no more space to place QDs! -> exit loop!')
break

我所做的是为我的圆创建直径,也使用在任意间隔内均匀分布的数字,这使我的累积分布函数变得平滑。这是我需要的解决方案,但它可能不太适合最初的问题(或者问题最初的表述不准确:p(

最新更新