`rand()'的有用性 - 或者谁应该调用`srand()`? [英] Usefulness of `rand()` - or who should call `srand()`?

查看:255
本文介绍了`rand()'的有用性 - 或者谁应该调用`srand()`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:我使用 rand() std :: rand() std :: random_shuffle()和其他函数在我的科学计算的代码。为了能够重现我的结果,我总是显式指定随机种子,并通过 srand()设置。这工作正常,直到最近,当我想出libxml2也会调用 srand()延迟其第一次使用 - 这是在我的早期 srand )



我填写了

srand() blockquote>

然后首先初始化libxml2。
这是一个完全合法的调用,从图书馆。你应该
不指望没有人调用 srand(),并且手册页无处
说使用 srand 应该避免多次。


这实际上是我的问题。如果一般的策略是每个lib可以/应/会调用 srand(),我可以/也可以在这里和那里调用它,我真的不知道这可能是有用的。或者如何 rand()有用呢?



这就是为什么我想,一般没有任何库应该调用 srand(),应用程序应该在开始时只调用一次。 (不考虑多线程,我想在这种情况下,你反正应该使用不同的东西。)



我也试图研究一些其他库实际调用 srand(),但我没有找到任何。有没有?



我当前的解决方法是这个丑陋的代码:

  {
//在第一次调用xmlDictCreate时,
// libxml2将初始化一些内部随机化系统,
//调用srand(time(NULL))。
//所以,首先调用这里,这样我们可以使用我们的
//自己的随机种子。
xmlDictPtr p = xmlDictCreate();
xmlDictFree(p);
}

srand(my_own_seed);

可能唯一的清洁解决方案是不使用它,只使用我自己的随机生成器(也许通过 C ++ 11 < random> )。但这不是真正的问题。 问题是,谁应该调用 srand(),如果每个人都这样做, rand() 使用新的< random> header。它允许多个引擎实例,使用不同的算法,更重要的是为你,独立的种子。




为了回答有用的 code> rand
生成随机数字。这就是它的好处。如果你需要细粒度的控制,包括再现性,你不应该只有一个已知的种子,但已知的算法。 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.

I filled in a bug report to libxml2 about its srand() call, but I got the answer:

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 using srand() multiple time should be avoided

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?

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.)

I also tried to research a bit which other libraries actually call srand(), but I didn't find any. Are there any?

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);

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?

解决方案

Use the new <random> header instead. It allows for multiple engine instances, using different algorithms and more importantly for you, independent seeds.

[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.

这篇关于`rand()'的有用性 - 或者谁应该调用`srand()`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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