生成在圆中均匀分布的随机点

/// <summary>
/// 生成在圆中均匀分布的随机点
/// </summary>
/// <param name="radius">圆的半径</param>
/// <param name="random">种子</param>
/// <returns></returns>
public static PointF GenerateCircleRandomPoint(float radius, Random random)
{
    // 角度 theta
    var theta = random.NextDouble() * Math.PI * 2;
    // 生成距离圆心的长度
    var len = Math.Sqrt(random.NextDouble()) * radius;
    var x = Math.Cos(theta) * len;
    var y = Math.Sin(theta) * len;
    return new PointF((float)x, (float)y);
}
int sumCount = 100_000_000;
Random random = new Random();
WSCommFunc.PrintThreadId("Create data...........");
List<DemoItem> testLis = new List<DemoItem>(sumCount);
for (int i = 0; i < sumCount; i++)
{
    var pt = WSCommFunc.GenerateCircleRandomPoint(130, random);
    testLis.Add(new DemoItem()
    {
        X = pt.X,
        Y = pt.Y,
    });
}
posted @ 2024-03-27 10:19  wesson2019  阅读(23)  评论(0编辑  收藏  举报