`兰特()`的实用性 - 或者谁应该调用`srand()函数`? [英] Usefulness of `rand()` - or who should call `srand()`?
问题描述
背景:我用兰特()
,的std ::兰特()
,的std :: random_shuffle()
在我的code为科学计算等功能。为了能够复制我的结果,我总是明确地指定随机种子,并通过函数srand设置()
。这工作得很好,直到最近,当我想通了,libxml2的也叫函数srand()
懒洋洋地在其第一次使用 - 这是我早期的函数srand之后( )
电话。
Background: I use rand()
, std::rand()
, std::random_shuffle()
and other functions in my code for scientific calculations. To be able to reproduce my results, I always explicitly specify the random seed, and set it via srand()
. That worked fine until recently, when I figured out that libxml2 would also call srand()
lazily on its first usage - which was after my early srand()
call.
我填写了 bug报告有关其的libxml2函数srand()
打电话,但我得到了答案:
I filled in a bug report to libxml2 about its srand()
call, but I got the answer:
初始化的libxml2第一呢。
这是可以从库中做了一个完全合法的电话。你应该
不要指望别人通话函数srand()
,无处手册页
指出,使用函数srand()
多个时,应避免
Initialize libxml2 first then. That's a perfectly legal call to be made from a library. You should not expect that nobody else calls
srand()
, and the man page nowhere states that usingsrand()
multiple time should be avoided
这其实是现在我的问题。如果是一般的政策是,每LIB可以/应该/将调用函数srand()
,我可以/也可以在这里和那里调用它,我实在不明白怎么样这可能是有用的。要不怎么是兰特()
有用呢?
This is actually my question now. If the general policy is that every lib can/should/will call srand()
, and I can/might also call it here and there, I don't really see how that can be useful at all. Or how is rand()
useful then?
这就是为什么我认为,一般的(书面)政策,是任何LIB应该永远叫函数srand()
键,应用程序应该在开始的时候只有一次调用它。 (不采取多线程考虑。我想在这种情况下,你反正应该使用不同的东西。)
That is why I thought, the general (unwritten) policy is that no lib should ever call srand()
and the application should call it only once in the beginning. (Not taking multi-threading into account. I guess in that case, you anyway should use something different.)
我也试着研究了一下它的其他库实际调用函数srand()
,但我没有发现任何。是否有任何?
I also tried to research a bit which other libraries actually call srand()
, but I didn't find any. Are there any?
我目前的解决办法是这个丑陋的code:
My current workaround is this ugly code:
{
// On the first call to xmlDictCreate,
// libxml2 will initialize some internal randomize system,
// which calls srand(time(NULL)).
// So, do that first call here now, so that we can use our
// own random seed.
xmlDictPtr p = xmlDictCreate();
xmlDictFree(p);
}
srand(my_own_seed);
大概是唯一干净的解决办法是不使用,在所有的,只有用自己的随机数发生器(通过的 C ++ 11 <随机>
)。但是,这并不是真正的问题。 现在的问题是,谁应该叫函数srand()
,如果人人都做,怎么会是兰特()
那么有用吗?
Probably the only clean solution would be to not use that at all and only to use my own random generator (maybe via C++11 <random>
). But that is not really the question. The question is, who should call srand()
, and if everyone does it, how is rand()
useful then?
推荐答案
使用新的&LT;随机&GT;
头而不是。它允许多个引擎的情况下,使用不同的算法,更重要的是你的,独立的种子。
Use the new <random>
header instead. It allows for multiple engine instances, using different algorithms and more importantly for you, independent seeds.
要回答有用的一部分,兰特
生成的随机数字。这就是它的良好的。如果您需要细粒度控制,包括重现性,你不应该只有一个已知的种子,但已知的算法。 函数srand
充其量给你一个固定的种子,所以这不是一个完整的解决方案呢。
[edit]
To answer the "useful" part, rand
generates random numbers. That's what it's good for. If you need fine-grained control, including reproducibility, you should not only have a known seed but a known algorithm. srand
at best gives you a fixed seed, so that's not a complete solution anyway.
这篇关于`兰特()`的实用性 - 或者谁应该调用`srand()函数`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!