Generate a random point within a circle (uniformly)

前端 未结 21 2828
逝去的感伤
逝去的感伤 2020-11-22 15:45

I need to generate a uniformly random point within a circle of radius R.

I realize that by just picking a uniformly random angle in the interval [0 ... 2π),

21条回答
  •  借酒劲吻你
    2020-11-22 16:16

    Here is my Python code to generate num random points from a circle of radius rad:

    import matplotlib.pyplot as plt
    import numpy as np
    rad = 10
    num = 1000
    
    t = np.random.uniform(0.0, 2.0*np.pi, num)
    r = rad * np.sqrt(np.random.uniform(0.0, 1.0, num))
    x = r * np.cos(t)
    y = r * np.sin(t)
    
    plt.plot(x, y, "ro", ms=1)
    plt.axis([-15, 15, -15, 15])
    plt.show()
    

提交回复
热议问题