C:约兰特,随机arc4random明确的真相 [英] C: the definitive truth about rand, random and arc4random

查看:103
本文介绍了C:约兰特,随机arc4random明确的真相的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有很多关于这个主题的相互矛盾的信息。因此,让我们尝试在一个明确的答案一致:

There's a lot of conflicting information about this topic. So let's try to agree on a definitive answer:

其中在这些C随机数发生器的一个创造更好的随机性:兰特,随机或arc4random

Which one of these random number generator in C create better randomness: rand, random or arc4random?

请注意:只是为了使问题清楚,这不是一个关于真正的随机性的问题,这只是那些3之间的冲突。

note: Just to make the question clear, this is not a question about true randomness, it's only a clash between those 3.


随着指出的那样,这个问题并没有太大的意义,因为这是不是C,但有关具体实施时,在我的情况,可可(更具体的iPhone SDK的,但我的猜测是,他们是相同的至于这些功能去)。不过,这里有一些有用的信息。我得出的结论通过实施,主要是因为它的易用性(不播种需要),这是没有一个人指出的重要因素arc4random。

As pointed out, this question doesn't make much sense, as this is not about C, but about a specific implementation, in my case, cocoa (more specifically the iphone sdk, but my guess is they are the same as far as these functions go). Still, there's some useful information here. I concluded by implementing arc4random, mostly because of its ease of use (no seeding needed), which is an important factor that no one pointed out.

我关闭的问题,并添加可可开发商寻找的随机数发生器信息可可标记。非常感谢那些谁贡献,并遗憾的混乱。

I'm closing the question, and adding the cocoa tag for cocoa developers looking for information on RNGs. Many thanks for those who contributed, and sorry for the confusion.

推荐答案

的这些功能​​中,只兰特是标准C的部分随机是POSIX的一部分,并且arc4random仅在BSD提供(和衍生的)。因此,只有兰特,在C。

Of these functions, only rand is part of standard C. random is part of POSIX, and arc4random is only provided in BSD (and derived). So only rand is "in C".

有关兰特,C标准只字未提发电机的质量,即返回总是相同的数量将符合。它说,该号码必须是0和RAND_MAX之间。正在使用RAND_MAX的值,和precise算法,都实现定义(虽然RAND_MAX必须至少32767)

For rand, the C standard says nothing about the quality of the generator, i.e. returning always the same number would be conforming. It says that the number must be between 0 and RAND_MAX. The value of RAND_MAX, and the precise algorithm being used, are implementation defined (although RAND_MAX must be at least 32767).

对于随机,POSIX 指定,它必须在默认情况下的周期至少2个^ 31,并且,如果INITSTATE被称为具有256字节的状态,那么它必须有一个时间至少2个^ 69;其他细节再次实现定义的。

For random, POSIX specifies that it must have a period of atleast 2^31 by default, and, if initstate is called with 256 bytes of state, then it must have a period of atleast 2^69; other details are again implementation-defined.

有关arc4random,具体实施是它的定义(RC4)的一部分。它规定,它使2 ^ 32个不同的价值观;我找不到任何东西的
期。

For arc4random, the specific implementation is part of its definition (RC4). It's specified that it gives 2^32 different values; I could not find anything about its period.

要详细地对它们进行比较,一个人必须要知道你指什么具体实施。

To compare them in detail, one would have to know what specific implementation you refer to.

这篇关于C:约兰特,随机arc4random明确的真相的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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