作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: https://leetcode.com/problems/generate-random-point-in-a-circle/description/

题目描述:

Given the radius and x-y positions of the center of a circle, write a function randPoint which generates a uniform random point in the circle.

Note:

  1. input and output values are in floating-point.
  2. radius and x-y position of the center of the circle is passed into the class constructor.
  3. a point on the circumference of the circle is considered to be in the circle.
  4. randPoint returns a size 2 array containing x-position and y-position of the random point, in that order.

Example 1:

Input:
["Solution","randPoint","randPoint","randPoint"]
[[1,0,0],[],[],[]]
Output: [null,[-0.72939,-0.65505],[-0.78502,-0.28626],[-0.83119,-0.19803]]

Example 2:

Input:
["Solution","randPoint","randPoint","randPoint"]
[[10,5,-7.5],[],[],[]]
Output: [null,[11.52438,-8.33273],[2.46992,-16.21705],[11.13430,-12.42337]]

Explanation of Input Syntax:

The input is two lists: the subroutines called and their arguments. Solution’s constructor has three arguments, the radius, x-position of the center, and y-position of the center of the circle. randPoint has no arguments. Arguments are always wrapped with a list, even if there aren’t any.

题目大意

给定圆的圆心和半径,生成落在这个圆里面的随机点。

解题方法

比较简单的方法是拒绝采样(Rejection Sampling),先在圆的外接圆内随机采点,然后判断这个点是不是在圆内,如果是的话就返回,否则再取一次。这个方法还可以用来估计圆周率。方法就不写了。

我直觉的方法还是使用极坐标的方式,随机找半径、随机找角度,然后就确定了一个点。但是没有通过最后一个测试用例。如果不查资料我是不会发现,这个做法是错的!

直接对半径和角度进行随机采样的方式会使得靠近圆心的点被取到的概率偏大,结果是取点的概率在这个圆内不是均匀的。为什么呢?因为题目要求的是对圆内任何面积上的点是均匀分布的,而不是对圆心出发的半径上是均匀分布的。那么以圆心为半径的小圆的范围内的点密度一定会比更大圆的密度大。

下图中左侧是随机生成半径和角度构成的点,右侧是随即生成的半径取根号和角度构成的点。

正确的做法是对边的随机数求根号。具体做法需要看478. Generate Random Point in a Circle(随机)

那么这个事情应该怎么理解呢?

首先我们要明白,题目要我们产生的是在圆内产生均匀的点,也就是说,对于圆中任意小的面积内落入点的概率相等。注意刚才说的是任意面积落点的概率是相等的。而如果采用随机半径+随机角度的方式,那么在任意半径上落入点的概率相等。很明显的是靠近圆心的半径比较密,远离圆心的时候半径比较稀疏。(类比一下太阳,太阳表面光线密度最大也就最亮,随着光线的传播,距离越远,光的亮度越小。)

右图这种对半径去根号的为什么是对的呢?因为我们产生的随机数是在0~1之间,那么去了根号之后会使得生成的点在1附近的密度更大,在0附近的密度变小。直觉上来看,那就是使得半径最外边被取到的概率变大,在圆心附近被取到的概率变小了。从而修正了圆心取到的点更密集的问题。

时间复杂度是O(1),空间复杂度是O(1)。

class Solution:

    def __init__(self, radius, x_center, y_center):
"""
:type radius: float
:type x_center: float
:type y_center: float
"""
self.r = radius
self.x = x_center
self.y = y_center def randPoint(self):
"""
:rtype: List[float]
"""
nr = math.sqrt(random.random()) * self.r
alpha = random.random() * 2 * 3.141592653
newx = self.x + nr * math.cos(alpha)
newy = self.y + nr * math.sin(alpha)
return [newx, newy] # Your Solution object will be instantiated and called as such:
# obj = Solution(radius, x_center, y_center)
# param_1 = obj.randPoint()

C++代码如下:

class Solution {
public:
Solution(double radius, double x_center, double y_center) {
rad = radius;
x = x_center;
y = y_center;
} vector<double> randPoint() {
const double PI = 3.141592653;
double nr = sqrt(rand() / double(RAND_MAX)) * rad;
double alpha = rand() / double(RAND_MAX) * 2 * PI;
double nx = x + nr * cos(alpha);
double ny = y + nr * sin(alpha);
return {nx, ny};
}
private:
double rad;
double x, y;
}; /**
* Your Solution object will be instantiated and called as such:
* Solution obj = new Solution(radius, x_center, y_center);
* vector<double> param_1 = obj.randPoint();
*/

参考资料:

https://zhanghuimeng.github.io/post/leetcode-478-generate-random-point-in-a-circle/

日期

2018 年 10 月 14 日 —— 周赛做出来3个题,开心
2019 年 2 月 1 日 —— 2019已经过去了一个月

【LeetCode】478. Generate Random Point in a Circle 解题报告(Python & C++)的更多相关文章

  1. 478. Generate Random Point in a Circle

    1. 问题 给定一个圆的半径和圆心坐标,生成圆内点的坐标. 2. 思路 简单说 (1)在圆内随机取点不好做,但是如果画出这个圆的外接正方形,在正方形里面采样就好做了. (2)取两个random确定正方 ...

  2. 【LeetCode】497. Random Point in Non-overlapping Rectangles 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/random-p ...

  3. 【LeetCode】26. Remove Duplicates from Sorted Array 解题报告(Python&C++&Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 日期 [LeetCode] https:// ...

  4. 【LeetCode】450. Delete Node in a BST 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 迭代 日期 题目地址:https://leetcode ...

  5. 【LeetCode】380. Insert Delete GetRandom O(1) 解题报告(Python)

    [LeetCode]380. Insert Delete GetRandom O(1) 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxu ...

  6. 【LeetCode】95. Unique Binary Search Trees II 解题报告(Python)

    [LeetCode]95. Unique Binary Search Trees II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  7. 【LeetCode】Longest Word in Dictionary through Deleting 解题报告

    [LeetCode]Longest Word in Dictionary through Deleting 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode. ...

  8. 【LeetCode】129. Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  9. 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)

    [LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

随机推荐

  1. R语言与医学统计图形-【28】ggplot2扩展包ggrepel、ggsci、gganimate、ggpubr

    ggplot2绘图系统--扩展包ggrepel.ggsci.gganimate.ggpubr等 部分扩展包可在CRAN直接下载,有些需借助devtools包从Github下载. 1. ggrepel包 ...

  2. 查看nginx(Web网页服务器)状态是否正常

    Linux每个应用运行都会产生一个进程,那么我们就可以通过查看Nginx进程是否存在来判断它是否启动. 1.有时想知道nigix是否在正常运行,需要用linux命令查看nginx运行情况.执行命令:p ...

  3. Linux搭建rsync备份服务器备份

    环境: 1台rsync备份服务器,IP:10.0.0.188 1台rsync备份客户端,IP:10.0.0.51 备份数据需注意: 1. 在业务低谷时间进行备份 2. 进行备份限速 一.搭建rsync ...

  4. 转-nRF5 SDK for Mesh(六) BLE MESH 的 基础概念

    nRF5 SDK for Mesh(六) BLE MESH 的 基础概念 Basic Bluetooth Mesh concepts The Bluetooth Mesh is a profile s ...

  5. Oracle基础入门

    说明:钓鱼君昨天在网上找到一份oracle项目实战的文档,粗略看了一下大致内容,感觉自己很多知识不够扎实,便跟着文档敲了一遍,目前除了机械性代码没有实现外,主要涉及知识:创建表空间.创建用户.给用户赋 ...

  6. JDBC01 获取数据库连接

    概述 Java Database Connectivity(JDBC)直接访问数据库,通用的SQL数据库存取和操作的公共接口,定义访问数据库的标准java类库(java.sql,javax.sql) ...

  7. MySQL8.0配置文件详解

    mysql8.0配置文件一.关键配置1. 配置文件的位置 MySQL配置文件 /etc/my.cnf 或者 /etc/my.cnf.d/server.cnf几个关键的文件:.pid文件,记录了进程id ...

  8. Scala(七)【异常处理】

    目录 一.try-catch-finally 二.Try(表达式).getOrElse(异常出现返回的默认值) 三. 直接抛出异常 一.try-catch-finally 使用场景:在获取外部链接的时 ...

  9. java加密方式

    加密,是以某种特殊的算法改变原有的信息数据,使得未授权的用户即使获得了已加密的信息,但因不知解密的方法,仍然无法了解信息的内容.大体上分为双向加密和单向加密,而双向加密又分为对称加密和非对称加密(有些 ...

  10. Mysql 常见报错和疑问汇总

    1.初始化数据库的时候报错 error while loading shared libraries: libstdc++.so.5: cannot open shared object file:  ...