C-faq q13.20 [英] C-faq q13.20
问题描述
FAQ中q13.20中的一个例子
( http://c-faq.com/lib/sd16.html ,链接自
http://c-faq.com/lib/gaussian.html )似乎错了。
完整的代码就在那个链接上。实际上代码的部分工作是
y =(double)rand()/(RAND_MAX + 1.0); / * 0.0< = y< 1.0 * /
bin =(y <0.5)? 0:1;
y = fabs(y - 1.0); / * 0.0< y< = 1.0 * /
y = std_dev * sqrt(( - 2.0)* log(y));
返回bin? (mean + y):( mean-y);
代码应该生成高斯随机变量,但是
mean = 0和std_dev = 1它可以生成的最小数字是
sqrt(-2 * log(0.5)),约-1.177。分布是双峰的,并且
所以甚至不是模糊的高斯。
为了使这个消息更符合clc,我应该
可能还会增加编码狡辩:
bin =(y <0.5)? 0:1;
应该是
bin = y< 0.5;
-thomas
One of the examples in q13.20 in the FAQ
(http://c-faq.com/lib/sd16.html, linked from
http://c-faq.com/lib/gaussian.html) seems to be wrong.
The full code is at that link. The part of the code that actually does
the work is
y = (double) rand() / (RAND_MAX + 1.0); /* 0.0 <= y < 1.0 */
bin = (y < 0.5) ? 0 : 1;
y = fabs(y - 1.0); /* 0.0 < y <= 1.0 */
y = std_dev * sqrt((-2.0) * log(y));
return bin ? (mean + y) : (mean - y);
The code is supposed to generate Gaussian random variables, but with
mean=0 and std_dev=1 the smallest number it can generate is
sqrt(-2*log(0.5)), about -1.177. The distribution is two-humped, and
so not even vaguely Gaussian.
In the interests of making this message more c.l.c-conforming I should
probably also add a coding quibble:
bin = (y < 0.5) ? 0 : 1;
should just be
bin = y<0.5;
-thomas
推荐答案
Thomas Lumley写道:
Thomas Lumley wrote:
常见问题解答中q13.20中的一个例子
( http://c-faq.com/lib/sd16。 html ,链接自
http:// c- faq.com/lib/gaussian.html )似乎是错误的。
完整的代码就在那个链接上。实际上代码的部分工作是
y =(double)rand()/(RAND_MAX + 1.0); / * 0.0< = y< 1.0 * /
bin =(y <0.5)? 0:1;
y = fabs(y - 1.0); / * 0.0< y< = 1.0 * /
y = std_dev * sqrt(( - 2.0)* log(y));
返回bin? (平均值+ y):(平均值-y);
One of the examples in q13.20 in the FAQ
(http://c-faq.com/lib/sd16.html, linked from
http://c-faq.com/lib/gaussian.html) seems to be wrong.
The full code is at that link. The part of the code that actually does
the work is
y = (double) rand() / (RAND_MAX + 1.0); /* 0.0 <= y < 1.0 */
bin = (y < 0.5) ? 0 : 1;
y = fabs(y - 1.0); /* 0.0 < y <= 1.0 */
y = std_dev * sqrt((-2.0) * log(y));
return bin ? (mean + y) : (mean - y);
您的第一个链接不包含任何代码。第二个链接上的代码
不包含任何fabs()。
所以我不知道你在哪里找到你发布的代码
以上。
Your first link doesn''t contain any code. The code
at the second link does not contain anywhere fabs().
So I have no idea where you got the code you posted
above.
Spiros Bousbouras写道:
Spiros Bousbouras wrote:
Thomas Lumley写道:
Thomas Lumley wrote:
常见问题解答中q13.20中的一个例子
( http://c-faq.com/lib/sd16.html ,链接自
http://c-faq.com/lib/gaussian.html )似乎是错的。
完整代码就在那个链接上。实际上代码的部分工作是
y =(double)rand()/(RAND_MAX + 1.0); / * 0.0< = y< 1.0 * /
bin =(y <0.5)? 0:1;
y = fabs(y - 1.0); / * 0.0< y< = 1.0 * /
y = std_dev * sqrt(( - 2.0)* log(y));
返回bin? (平均值+ y):(平均值-y);
One of the examples in q13.20 in the FAQ
(http://c-faq.com/lib/sd16.html, linked from
http://c-faq.com/lib/gaussian.html) seems to be wrong.
The full code is at that link. The part of the code that actually does
the work is
y = (double) rand() / (RAND_MAX + 1.0); /* 0.0 <= y < 1.0 */
bin = (y < 0.5) ? 0 : 1;
y = fabs(y - 1.0); /* 0.0 < y <= 1.0 */
y = std_dev * sqrt((-2.0) * log(y));
return bin ? (mean + y) : (mean - y);
您的第一个链接不包含任何代码。
Your first link doesn''t contain any code.
再看一遍。这里有他正在谈论的代码的链接。
Look again. There''s a link to the code he''s talking about.
注册用户写道:
Registered User wrote:
Spiros Bousbouras写道:
Spiros Bousbouras wrote:
Thomas Lumley写道:
Thomas Lumley wrote:
q13中的一个例子.20在常见问题中
( http:// c-faq.com/lib/sd16.html ,链接自
http://c-faq.com/lib/gaussian.html )似乎错了。
>
完整代码是在那个链接。实际上代码的一部分
工作是
>
y =(double)rand()/(RAND_MAX + 1.0) ; / * 0.0< = y< 1.0 * /
bin =(y <0.5)? 0:1;
y = fabs(y - 1.0); / * 0.0< y< = 1.0 * /
y = std_dev * sqrt(( - 2.0)* log(y));
>
返回斌? (平均值+ y):(平均值-y);
One of the examples in q13.20 in the FAQ
(http://c-faq.com/lib/sd16.html, linked from
http://c-faq.com/lib/gaussian.html) seems to be wrong.
>
The full code is at that link. The part of the code that actually does
the work is
>
y = (double) rand() / (RAND_MAX + 1.0); /* 0.0 <= y < 1.0 */
bin = (y < 0.5) ? 0 : 1;
y = fabs(y - 1.0); /* 0.0 < y <= 1.0 */
y = std_dev * sqrt((-2.0) * log(y));
>
return bin ? (mean + y) : (mean - y);
您的第一个链接不包含任何代码。
Your first link doesn''t contain any code.
再看一遍。他正在谈论的代码有一个链接。
Look again. There''s a link to the code he''s talking about.
我又看了一眼;我找不到。
I looked again ; I can''t find it.
这篇关于C-faq q13.20的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!