javascript - 圆内到处都是随机点
问题描述
我正在使用下面的代码随机生成一些位于圆圈内的 x,ys:
// 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。我怎样才能做到这一点?
解决方案
由于雅可比行列式,你必须取平方根
r = R * Math.sqrt(Math.random());
推荐阅读
- javascript - 无论用户字体大小如何,如何使文本适合按钮?
- logic - 当两个输入短路时,两个输入 XOR 门将如何表现?
- prometheus - 为什么 Prometheus increase() 函数不考虑计数器重置?
- python - 在 python 中实现 vtkLagrangianParticleTracker
- azure-active-directory - 使用 Graph 读取所有 Azure AD 用户
- amazon-s3 - 为什么boto3 head_object即使存在也不返回元数据?
- pine-script - Trading View Pine 脚本将值添加到现有变量值以避免警报 Stopper -- 触发次数过多
- javascript - 通过脚本部分更新谷歌网络应用程序前端
- c++ - 使用 OpenMP 的 C++ DLL 程序崩溃
- sql - SQL 将卷拆分为不重复存储桶 ID 的存储桶