How to generate random points in a circular distribution

前端 未结 6 1712
执笔经年
执笔经年 2021-02-02 17:54

I am wondering how i could generate random numbers that appear in a circular distribution.

I am able to generate random points in a rectangular distribution such that

6条回答
  •  暖寄归人
    2021-02-02 18:11

    I would use polar coordinates:

    r_squared, theta = [random.randint(0,250000), 2*math.pi*random.random()]

    Then r is always less than or equal to the radius, and theta always between 0 and 2*pi radians.

    Since r is not at the origin, you will always convert it to a vector centered at 500, 500, if I understand correctly

    x = 500 + math.sqrt(r_squared)*math.cos(theta) y = 500 + math.sqrt(r_squared)*math.sin(theta)

    Choose r_squared randomly because of this

提交回复
热议问题