How to generate random points in a circular distribution

前端 未结 6 1692
执笔经年
执笔经年 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:24

    What you need is to sample from (polar form):

    r, theta = [math.sqrt(random.randint(0,500))*math.sqrt(500), 2*math.pi*random.random()]
    

    You can then transform r and theta back to cartesian coordinates x and y via

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

    Related (although not Python), but gives the idea.

提交回复
热议问题