【问题标题】:How do I program a spatially dependend random number distribution?如何编程空间相关的随机数分布?
【发布时间】:2021-01-13 20:37:42
【问题描述】:

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

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之间出现一定程度。

有什么想法可以做到吗?

【问题讨论】:

    标签: python numpy random montecarlo


    【解决方案1】:

    这是一个想法。

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

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

    请注意,1/20 是均匀 (0, 1) 随机数的 20 次方根。尝试将 1/20 更改为不同的值,看看会发生什么。

    这种方式还有其他选择半径的方法,都可以用一个概率密度函数来描述(更多信息请看下面的回答:Generate a random point within a circle (uniformly),它展示了一个线性密度函数导致圆内点的均匀分布)。

    【讨论】:

      【解决方案2】:

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

      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)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-02
        • 2011-05-26
        • 1970-01-01
        • 2020-12-05
        • 2016-02-09
        • 2016-11-09
        • 1970-01-01
        • 2016-07-04
        相关资源
        最近更新 更多