python 中圆圈内除外的随机数生成器
random numer generation except inside of the circle in python
我想在二维数据x(0,1)和y(0,1)中生成750个随机数
X1 = np.random.random((750,2))
但是,我想确保我在圆形区域中没有任何值,例如
我可以删除该值,但我想将随机数固定为 750。生成此类列表的最佳方法是什么?
你可以直接应用毕达哥拉斯定理。这是我的代码
import numpy as np
import matplotlib.pyplot as plt
dist = 0.2
X1 = np.random.random((750,2))
X2 = [x for x in X1 if (x[0]-0.5)**2 + (x[1]-0.5)**2 > dist**2] # filter values that are to close to center
# for plotting
x = [x[0] for x in X2]
y = [y[1] for y in X2]
plt.scatter(x,y)
plt.show()
import random
points = []
radius = 1
num_points=1500
index = 0
max_coord = 750
while index < num_points:
x=random.random()*max_coord
y=random.random()*max_coord
if x**2 + y**2 > radius**2:
points = points + [[x,y]]
index = index + 1
print(points)
更改max_coor以获得更大的数字。这只会给您在给定半径 = 1 的圆之外的 1500 个点。每个坐标在 0 和 750 之间变化
您可以使用一种名为 rejection sampling. Generate candidate values and see if they meet your constraints. If so, accept them, otherwise reject and repeat. With 2-d sampling, the probability of acceptance on a given trial is P{Accept} = Area(acceptance region) / Area(generating region)
. Repetitions due to rejection are independent, so the number of trials has a geometric distribution 且参数为 p = P{Accept}
的著名技术来执行此操作
和 Expected # trials = 1 / P{Accept}
。例如,如果拒绝区域是一个以单位正方形为中心的半径为 1/2 的圆,P{Accept} = (4 - Pi)/4
平均每个生成的值需要 4/(4 - Pi) (approximately 4.66)
次尝试。显然,如果半径 >= sqrt(2)/2,这将不起作用,并且当您接近该限制时会变得昂贵。
请注意,拒绝圆不需要以正方形为中心,您只需拒绝距离圆心小于半径的任何候选人。
代码如下:
import numpy as np
def generate_pt(exclusion_radius):
while True:
candidate = np.random.sample(2)
# The following assumes the rejection circle is centered at (0.5, 0.5).
# Adjust by subtracting individual x/y coordinates for the center of
# the circle if you don't want it centered. Acceptance/rejection
# distance is determined by Pythagorean theorem.
if np.sum(np.square(candidate - 0.5)) >= exclusion_radius * exclusion_radius:
return candidate
# Generate 750 points outside the centered circle of radius 0.3.
data = np.array([generate_pt(0.3) for _ in range(750)])
我想在二维数据x(0,1)和y(0,1)中生成750个随机数
X1 = np.random.random((750,2))
但是,我想确保我在圆形区域中没有任何值,例如
我可以删除该值,但我想将随机数固定为 750。生成此类列表的最佳方法是什么?
你可以直接应用毕达哥拉斯定理。这是我的代码
import numpy as np
import matplotlib.pyplot as plt
dist = 0.2
X1 = np.random.random((750,2))
X2 = [x for x in X1 if (x[0]-0.5)**2 + (x[1]-0.5)**2 > dist**2] # filter values that are to close to center
# for plotting
x = [x[0] for x in X2]
y = [y[1] for y in X2]
plt.scatter(x,y)
plt.show()
import random
points = []
radius = 1
num_points=1500
index = 0
max_coord = 750
while index < num_points:
x=random.random()*max_coord
y=random.random()*max_coord
if x**2 + y**2 > radius**2:
points = points + [[x,y]]
index = index + 1
print(points)
更改max_coor以获得更大的数字。这只会给您在给定半径 = 1 的圆之外的 1500 个点。每个坐标在 0 和 750 之间变化
您可以使用一种名为 rejection sampling. Generate candidate values and see if they meet your constraints. If so, accept them, otherwise reject and repeat. With 2-d sampling, the probability of acceptance on a given trial is P{Accept} = Area(acceptance region) / Area(generating region)
. Repetitions due to rejection are independent, so the number of trials has a geometric distribution 且参数为 p = P{Accept}
的著名技术来执行此操作
和 Expected # trials = 1 / P{Accept}
。例如,如果拒绝区域是一个以单位正方形为中心的半径为 1/2 的圆,P{Accept} = (4 - Pi)/4
平均每个生成的值需要 4/(4 - Pi) (approximately 4.66)
次尝试。显然,如果半径 >= sqrt(2)/2,这将不起作用,并且当您接近该限制时会变得昂贵。
请注意,拒绝圆不需要以正方形为中心,您只需拒绝距离圆心小于半径的任何候选人。
代码如下:
import numpy as np
def generate_pt(exclusion_radius):
while True:
candidate = np.random.sample(2)
# The following assumes the rejection circle is centered at (0.5, 0.5).
# Adjust by subtracting individual x/y coordinates for the center of
# the circle if you don't want it centered. Acceptance/rejection
# distance is determined by Pythagorean theorem.
if np.sum(np.square(candidate - 0.5)) >= exclusion_radius * exclusion_radius:
return candidate
# Generate 750 points outside the centered circle of radius 0.3.
data = np.array([generate_pt(0.3) for _ in range(750)])