rand()是否在OSX上返回零? [英] Does rand() ever return zero on OSX?
问题描述
我已经在没有爱的情况下运行了将近十个小时的代码:
I have been running this code for almost ten hours with no love:
while ( true ) {
int r = rand();
assert( r != 0 );
}
我希望rand()最终会变成零,从而触发断言.
I expect rand() to eventually roll a zero and thus trigger the assert.
我是在做错什么还是rand()永远不会返回零?
Am I doing something wrong or does rand() never return zero?
还是我没有等很久才期待看到它?我使用的是2012年老式2GHz i7.
Or have I not been waiting long enough to expect to see it? I'm on 2012-vintage 2GHz i7.
推荐答案
If this link is the definition of Mac OS X rand()
, then 0 will never be produced. It's not a very good pseudo random number generator, IMHO.
除其他缺陷外,它的状态是先前返回的值,这意味着它是从上一个值到下一个值的严格函数;它的周期不能大于其范围,即[1, RAND_MAX-1]
. (它不能产生0,因为0在算法中是一个定点.)
Amongst its other flaws, its state is the previously returned value, which means that it is a strict function from previous value to next value; it's cycle cannot be larger than its range, which is [1, RAND_MAX-1]
. (It cannot produce 0 because 0 is a fixed-point in the algorithm.)
这篇关于rand()是否在OSX上返回零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!