我正在尝试在一个环形区域内生成随机的x和y坐标,该环形区域具有外半径为3.5和内半径为2。因此,对于x和y,必须满足以下条件:
x**2 + y**2 < 12.25 and x**2 + y**2 > 4
我编写了以下函数:
def meteorites():
circle = False
while circle == False:
r = np.array([uniform(-6., 6.), uniform(-6., 6.)])
# we will regenerate random numbers untill the coordinates
# are within the ring x^2+y^2 < 3,5^2 and x^2+y^2 > 2^2
if (r[0]**2+r[1]**2 < 12.25) and (r[0]**2+r[1]**2 > 4.):
circle = True
else :
circle = False
return r[0], r[1]
x = np.zeros(1000)
y = np.zeros(1000)
for i in range(1000):
x[i] = meteorites()[0]
y[i] = meteorites()[1]
plt.scatter(x,y)
plt.show()
当我画出结果坐标后,我得到了从-3.5到3.5的正方形区域。我似乎找不到问题所在。我也不确定是编码错误还是数学问题。既然你们通常擅长于这两者,你能看出我在这里做错了什么吗?