简体   繁体   English

没有种子()的rand()的第一次调用->相同的数字?

[英]First call of rand() without seed() --> same number?

I am looking through ac program that is available for several OSs and produces inconsistent results among platforms for the exact same data. 我正在寻找可用于多个操作系统的ac程序,这些程序在平台之间对于完全相同的数据会产生不一致的结果。 Looking because I lack some libraries to compile and debug it on my system. 因为我缺少一些可以在系统上进行编译和调试的库。

I noticed that it calls rand() during initialization without calling srand(), probably because it needs only 1 random value. 我注意到它在初始化期间调用了rand()而不调用srand(),可能是因为它只需要1个随机值。 Now, I was wondering how does rand() behave in this case? 现在,我想知道rand()在这种情况下的行为如何? Does it takes the operating system initial value of the random library ? 它是否需要随机库的操作系统初始值? I am currently hoping that this might explain the different results per platform, but I lack background knowledge to rand() and what happens if it is called without srand(). 我目前希望这可以解释每个平台的不同结果,但是我缺乏rand()的背景知识以及如果不使用srand()调用会发生什么情况。

If it always returns an default-init value of the OS, it would explain why different platforms produces different results. 如果它总是返回操作系统的default-init值,它将解释为什么不同的平台会产生不同的结果。

Thx for any input and sorry for the theoretic question - no code ^^. 谢谢任何输入,对理论问题感到抱歉-无代码^^。

From rand() 's manpage: rand()的联机帮助页中:

If no seed value is provided, the rand() function is automatically seeded with a value of 1. 如果没有提供种子值,则rand()函数将自动以1值作为种子。

So, in a given system, it will always give you the same sequence. 因此,在给定的系统中,它将始终为您提供相同的顺序。 However, even if the seed is the same, different platforms may implement different random generators, and thus yield different sequences. 但是,即使种子是相同的,不同的平台也可能实现不同的随机生成器,从而产生不同的序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM