大多数现代计算机都表现出非确定性行为,这使得无法判断两次连续调用以读取计算机时钟之间会发生多少时钟周期。
下面的代码是一个使用计算机时钟的一个字节的伪随机数生成器。
unsigned long occurrences = 0;
unsigned long total = 0;
while (true) {
if ((clock() & 0xFF) == 60) // testing ocurrences for a given number, 60 for instance
occurrences++;
total++;
printf("%f\n", (float)occurrences / (float)total ); // this should be approximately 1/256 = 0.00390625
}
不包括诸如加密之类的严肃应用,它可以用于游戏的移动平台。
我想知道这种实施的优点和缺点是什么。