C:关于rand,random和arc4random的最终真理 [英] C: the definitive truth about rand, random and arc4random

查看:231
本文介绍了C:关于rand,random和arc4random的最终真理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个主题有很多相冲突的资讯。因此,让我们尝试同意一个确定的答案:

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

在C中的哪个随机数生成器创建更好的随机性:rand,random或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.

问题,以及为寻找RNG信息的可可开发商添加可可标签。

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.

推荐答案

在这些函数中,只有rand是标准C的一部分。 random是POSIX的一部分,arc4random仅在BSD(和派生)中提供。因此,只有rand是in 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".

对于rand,C标准对发生器的质量没有任何说明,即返回总是相同的数字将符合。它说,数字必须在0和RAND_MAX之间。 RAND_MAX的值和所使用的精确算法是实现定义的(虽然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:关于rand,random和arc4random的最终真理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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