Generate a random point within a circle (uniformly)

前端 未结 21 2888
逝去的感伤
逝去的感伤 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:31

    Solution in Java and the distribution example (2000 points)

    public void getRandomPointInCircle() {
        double t = 2 * Math.PI * Math.random();
        double r = Math.sqrt(Math.random());
        double x = r * Math.cos(t);
        double y = r * Math.sin(t);
        System.out.println(x);
        System.out.println(y);
    }
    

    based on previus solution https://stackoverflow.com/a/5838055/5224246 from @sigfpe

提交回复
热议问题