我想知道如何生成出现在圆形分布中的随机数。
我能够在矩形分布中生成随机点,以便这些点在 (0
我将如何继续生成圆内的点,以便:
(x−500)^2 + (y−500)^2
import random
import math
# radius of the circle
circle_r = 10
# center of the circle (x, y)
circle_x = 5
circle_y = 7
# random angle
alpha = 2 * math.pi * random.random()
# random radius
r = circle_r * math.sqrt(random.random())
# calculating coordinates
x = r * math.cos(alpha) + circle_x
y = r * math.sin(alpha) + circle_y
print("Random point", (x, y))
在你的例子中circle_x
是 500 作为circle_y
is. circle_r
is 500.
计算半径以获得均匀分布点的另一个版本,基于这个答案 https://stackoverflow.com/a/5838055/4907653
u = random.random() + random.random()
r = circle_r * (2 - u if u > 1 else u)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)