为什么drand48()和朋友过时了? [英] Why are drand48() and friends obsolete?

查看:371
本文介绍了为什么drand48()和朋友过时了?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

毕竟,它们似乎优于标准libc rand().我错过了什么吗?

(我花了一些时间在网上搜索该问题,而我能找到的这个问题的另一个实例是在分配偏差的背景下,未得到解答.)

关于rand()和drand48()的手册页似乎也不尽相同.第一个建议第二个,第二个指出它已经过时,应该使用第一个. (不过,公平地说,许多了解PRNG背后的数学知识的人对于这些功能的手册页有疑问,因为它们的用词不当,在某些情况下甚至是错误的.)

不过,我找不到过时"状态的理由.

解决方案

我系统上的手册页(来自 Linux手册页项目说:

这些功能已被SVID 3声明为过时,其中指出: 应该使用rand(3).

SVID 3于1989年发布.

SVID 4 (链接为720页的PDF ),于1995年发布,文档drand48erand48lrand48nrand48mrand48jrand48srand48seed48lcong48,并且像POSIX一样,什么也没说他们已经过时了.

截至2013年,

POSIX 并没有说明它们已过时,过时或过时.

我还没有找到SVID 3的副本,所以我不知道为什么为什么会宣布这些功能已过时,但是显然稍后会重新考虑该决定.手册页中的语句似乎是过时的信息.我不用担心.

对于应该使用哪个函数,C标准rand()函数是最可移植的(除非您重新编译与源代码不同的函数).某些rand()实现的质量很差,低阶位以非常规则的模式重复;其他人稍微好一点.

如果您不需要高质量的伪随机数,则不妨使用rand()(通过以合理的值(例如srand(time(NULL))调用srand()进行播种).

如果您确实需要高质量的伪随机数,那么这些功能中的 none 可能就足够好了,例如,我建议您不要将它们中的任何一个用于加密.如果系统支持,可以使用/dev/urandom/dev/random.我听说过有关 Merenne Twister 的好事,但我缺乏进一步评论的专业知识. /p>

(顺便说一句,如果您在Google搜索SVID,请注意Svið).

After all, they seem superior to the standard libc rand(). Have I missed something?

(I have spent some time searching for this online, and the only other instance of this question I could find was in the context of distribution bias and left unanswered.)

The manual pages for rand() and drand48() also seem at odds. The first recommends the second, and the second states that it is obsolete and the first should be used. (Though, to be fair, a lot of people who understand the math behind PRNGs have issues with the man pages for these functions because they are poorly-worded and in some instances just wrong.)

Still, I can find no justification for the "obsolete" status.

解决方案

The man page on my system (which is from the Linux man-pages project says:

These functions are declared obsolete by SVID 3, which states that rand(3) should be used instead.

SVID 3 was published in 1989.

SVID 4 (the link is to a 720-page PDF), published in 1995, documents drand48, erand48, lrand48, nrand48, mrand48, jrand48, srand48, seed48, and lcong48 and, like POSIX, says nothing about them being obsolete.

POSIX, as of 2013, says nothing about them being obsolete, obsolescent, or deprecated.

I haven't found a copy of SVID 3, so I don't know why it would have declared those functions obsolete, but apparently that decision was reconsidered later. The statement in the man page seems like out-of-date information. I wouldn't worry about it.

As for which function you should use, the C standard rand() function is the most portable (unless you recompile a different one from source code). Some rand() implementations are poor quality, with the low-order bits repeating in a very regular pattern; others are slightly better.

If you don't need high quality pseudo-random numbers, you might as well use rand() (seeded by calling srand() with a reasonable value, e.g., srand(time(NULL)).

If you do need high-quality pseudo-random numbers, it's likely that none of these functions is good enough, I'd advise against using any of them for cryptography, for example. You can use /dev/urandom or /dev/random if your system supports it. I've heard good things about the Mersenne Twister, but I lack the expertise to comment further.

(BTW, if you do a Google search for SVID, watch out for Svið).

这篇关于为什么drand48()和朋友过时了?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆