【发布时间】:2018-08-07 12:50:24
【问题描述】:
我正在使用以下代码随机生成一些位于圆圈内的 x,y :
// r is distance from the center and a is angle
// R is radius of circle
// M is center
r = R * Math.random();
a = 2 * Math.PI * Math.random();
x = Math.round(r * Math.cos(a) + M);
y = Math.round(r * Math.sin(a) + M);
问题是当越来越接近圆心时,在该位置获得 x,y 的机会越来越多。
但我正在寻找的只是在圆圈中完全随机的 x,y。我怎样才能做到这一点?
【问题讨论】:
-
关于这个问题的有趣讨论:Bertrand paradox(维基百科)
标签: javascript math random polar-coordinates