美文网首页leetcode算法
478. 在圆内随机生成点

478. 在圆内随机生成点

作者: 刘翊扬 | 来源:发表于2022-06-05 10:41 被阅读0次

给定圆的半径和圆心的位置,实现函数 randPoint ,在圆中产生均匀随机点。

实现 Solution 类:

Solution(double radius, double x_center, double y_center) 用圆的半径 radius 和圆心的位置 (x_center, y_center) 初始化对象
randPoint() 返回圆内的一个随机点。圆周上的一点被认为在圆内。答案作为数组返回 [x, y] 。

示例 1:

输入:
["Solution","randPoint","randPoint","randPoint"]
[[1.0, 0.0, 0.0], [], [], []]
输出: [null, [-0.02493, -0.38077], [0.82314, 0.38945], [0.36572, 0.17248]]
解释:
Solution solution = new Solution(1.0, 0.0, 0.0);
solution.randPoint ();//返回[-0.02493,-0.38077]
solution.randPoint ();//返回[0.82314,0.38945]
solution.randPoint ();//返回[0.36572,0.17248]

提示:

  • 0 < radius <= 108
  • -107 <= x_center, y_center <= 107
  • randPoint 最多被调用 3 * 104 次

思路

利用勾股定理 x * x + y + y <= radius * redius。

方法一:拒绝采样

image.png
class Solution {
    Random random = new Random();
    double radius;
    double x_center ;
    double y_center ;
    public Solution(double radius, double x_center, double y_center) {
        this.radius = radius;
        this.x_center = x_center ;
        this.y_center = y_center ;
    }
    
    public double[] randPoint() {
        while(true){
            double randomX = random.nextDouble() * (radius * 2) - radius ;
            double randomY = random.nextDouble() * (radius * 2) - radius;
            if (randomX * randomX + randomY * randomY <= radius * radius)
                return new double[]{randomX + x_center , randomY + y_center};
        }
    }
}

参考答案:自己写的

class Solution {

    double radius;
    double x_center;
    double y_center;

    Random random = new Random();

    public Solution(double radius, double x_center, double y_center) {
        this.radius = radius;
        this.x_center = x_center;
        this.y_center = y_center;
    }
    
    public double[] randPoint() {
        double x = random.nextDouble() * radius;
        double y = random.nextDouble() * radius;
        Double r = radius * radius;
        // double 类型比较大小使用 compareTo。本题,直接比较也是可以通过的
       while (r.compareTo(x * x + y * y) < 0) {
            x = random.nextDouble() * radius;
            y = random.nextDouble() * radius;
        }
        // 总共 4 个象限
        int i = random.nextInt(4) + 1;
        switch (i) {
            case 1:
                x = -x;
                break;
            case 2:
                break;
            case 3:
                x = -x;
                y = -y;
                break;
            case 4:
                y = -y;
                break;
            default:
        }
        x += x_center;
        y += y_center;
        return new double[]{x, y};
    }
}

/**
 * Your Solution object will be instantiated and called as such:
 * Solution obj = new Solution(radius, x_center, y_center);
 * double[] param_1 = obj.randPoint();
 */

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/generate-random-point-in-a-circle
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

相关文章

  • 478. 在圆内随机生成点

    给定圆的半径和圆心的位置,实现函数 randPoint ,在圆中产生均匀随机点。 实现 Solution 类: S...

  • 478. 在圆内随机生成点(Python)

    题目 难度:★★☆☆☆类型:几何方法:拒绝采样 力扣链接请移步本题传送门[https://leetcode-cn....

  • 每日一题-478. 在圆内随机生成点

    给定圆的半径和圆心的位置,实现函数 randPoint ,在圆中产生均匀随机点。 实现 Solution 类: S...

  • Excel怎么生成随机数?

    Excel怎么生成随机数?如何生成指定范围内的随机数呢?可以在Excel中使用函数,生成随机数。 1、在单元格中输...

  • Mock

    Versionmockjs: 1.0.1-beta3 mock 生成随机数组, 数组内数据部分随机 生成数组结果 ...

  • JS-工具类

    随机数Math.random()用来生成[0,1)之间的随机小数。生成特定范围内的随机数据区间Math.floor...

  • Qt 随机数的生成

    1、生成一定范围内的随机数

  • Python小函数

    生成随机数 生成指定范围内的浮点数: 参考链接:python生成随机数方法小结 进制转换 二进制到十进制:

  • Python——random模块

    random 随机数 randint(range) 生成范围内的随机数 randrange(range) 不包含最...

  • 自动化你的测试(二)

    问题 在两个点间移动时可能出错 黑盒验证算法的完备性 测试方法 在一个空间范围内随机生成点 空间范围选取比较小以确...

网友评论

    本文标题:478. 在圆内随机生成点

    本文链接:https://www.haomeiwen.com/subject/yrlrmrtx.html