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 呢? : 多谢.
|
|
|
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)
|