由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 给一个 [0, 1]区间上的 uniform distribution
相关主题
问问一道关于概率的题Epic 店面被考到coding的题目。。。(面经)
如果给随即函数rand[1,5] 如何产生rand[1,7]请问一道概率题,谢谢了先
讨论一道经典题[合集] Re: 贡献两个智力题,攒RP ( QUALCOMM)
请教一道面试题[合集] 贡献两个智力题,攒RP ( QUALCOMM)
Google 2 phone interviews exposed + 求祝福一个概率+编程题。
问一道google面试题一道概率题目
请教个弱题:random generator: from 1~5 to 1~7一个经典的随机数的问题。求教。
关于那个随机数的问一个题目
相关话题的讨论汇总
话题: rand话题: uniform话题: double话题: coordinate
进入JobHunting版参与讨论
1 (共1页)
j*****y
发帖数: 1071
1
如何产生一个 单位圆内的 uniform distribution 呢?
多谢.
f*****e
发帖数: 2992
2
你学数学的应该很在行啊?!

【在 j*****y 的大作中提到】
: 如何产生一个 单位圆内的 uniform distribution 呢?
: 多谢.

M********5
发帖数: 715
3
#include "math.h"
double UniDisCircle(){
while(true){
double m = rand();
double n = rand();
double t = m*m + n*n;
if( r <= 1.0 )
return sqrt(t);
}
}

【在 j*****y 的大作中提到】
: 如何产生一个 单位圆内的 uniform distribution 呢?
: 多谢.

j*****y
发帖数: 1071
4
惭愧 :)

【在 f*****e 的大作中提到】
: 你学数学的应该很在行啊?!
j*****y
发帖数: 1071
5
多谢 。
不过这个只能产生 1/4 个圆内的吧, 负数坐标出不来阿.

【在 M********5 的大作中提到】
: #include "math.h"
: double UniDisCircle(){
: while(true){
: double m = rand();
: double n = rand();
: double t = m*m + n*n;
: if( r <= 1.0 )
: return sqrt(t);
: }
: }

l***i
发帖数: 1309
6
generate r and theta, then use (r,theta), polar coordinate?
M********5
发帖数: 715
7
你最后的结果是要二维的还是一维的?如果是一维的话只能是这样的吧?要是结果是二
维的也还蛮好改的。。。
#include "math.h"
struct Coordinate{
int x;
int y;
};
int sign(){
double n = rand();
if( n<=0.5 )
return -1;
else
return 1;
}
Coordinate UniDistCircle(){
Coordinate co;
while(true){
double m = rand();
double n = rand();
double t = m*m + n*n;
if( t<=1.0){
co.x = sign()*m;
co.y = sign()*n;
return co;
}
}
}

【在 j*****y 的大作中提到】
: 多谢 。
: 不过这个只能产生 1/4 个圆内的吧, 负数坐标出不来阿.

j*****y
发帖数: 1071
8
thanks.

【在 l***i 的大作中提到】
: generate r and theta, then use (r,theta), polar coordinate?
j*****y
发帖数: 1071
9
thanks.

【在 M********5 的大作中提到】
: 你最后的结果是要二维的还是一维的?如果是一维的话只能是这样的吧?要是结果是二
: 维的也还蛮好改的。。。
: #include "math.h"
: struct Coordinate{
: int x;
: int y;
: };
: int sign(){
: double n = rand();
: if( n<=0.5 )

l*******b
发帖数: 2586
10
reject sampling吧,感觉有限步uniform distribution太难

【在 j*****y 的大作中提到】
: 如何产生一个 单位圆内的 uniform distribution 呢?
: 多谢.

相关主题
问一道google面试题Epic 店面被考到coding的题目。。。(面经)
请教个弱题:random generator: from 1~5 to 1~7请问一道概率题,谢谢了先
关于那个随机数的[合集] Re: 贡献两个智力题,攒RP ( QUALCOMM)
进入JobHunting版参与讨论
M********5
发帖数: 715
11
我写的代码就是基于reject sampling的想法的

【在 l*******b 的大作中提到】
: reject sampling吧,感觉有限步uniform distribution太难
f*******n
发帖数: 12623
12
No. You can't just use uniform distribution for r, because that way it will
be too concentrated in the middle and too spread out at the edge.
r needs to be weighted to the higher numbers. Specifically, the probability
distribution of r needs to be P(r) = 2r. So that at r = 1, it is twice as
likely as at r = 0.5. This is because dx dy = r dr dθ in polar coordinates.
To do that you can use inverse transform sampling http://en.wikipedia.org/
wiki/Inverse_transform_sampling). The cumulative distribution of r is r^2.
Therefore, to generate r with the correct distribution, you need to take
sqrt(rand()).

【在 l***i 的大作中提到】
: generate r and theta, then use (r,theta), polar coordinate?
k*******a
发帖数: 772
13
generate random number r1, X=2*(r1-0.5)
generate random number r2, Y=2*(r2-0.5)
if X^2+Y^2<1 then return (X, Y)
p***l
发帖数: 586
14
uniform on the sphere can be generated by normaling gaussian
so
x = randn(2,1) (inverse cdf * rand(2,1))
then x/\|x\| will do

【在 j*****y 的大作中提到】
: 如何产生一个 单位圆内的 uniform distribution 呢?
: 多谢.

b***e
发帖数: 1419
15
It's just {r: sqrt(rand()), \theta: rand()*2*PI} in polar notation.

【在 l*******b 的大作中提到】
: reject sampling吧,感觉有限步uniform distribution太难
P*******b
发帖数: 1001
16
这个好像x^2 + y^2 总是小于等于1的吧。

【在 k*******a 的大作中提到】
: generate random number r1, X=2*(r1-0.5)
: generate random number r2, Y=2*(r2-0.5)
: if X^2+Y^2<1 then return (X, Y)

1 (共1页)
进入JobHunting版参与讨论
相关主题
问一个题目Google 2 phone interviews exposed + 求祝福
Pick k lines from a large file randomly uniformly distributed问一道google面试题
Yelp 面经请教个弱题:random generator: from 1~5 to 1~7
用rand5()产生rand7()关于那个随机数的
问问一道关于概率的题Epic 店面被考到coding的题目。。。(面经)
如果给随即函数rand[1,5] 如何产生rand[1,7]请问一道概率题,谢谢了先
讨论一道经典题[合集] Re: 贡献两个智力题,攒RP ( QUALCOMM)
请教一道面试题[合集] 贡献两个智力题,攒RP ( QUALCOMM)
相关话题的讨论汇总
话题: rand话题: uniform话题: double话题: coordinate