如何从 C++ rand() 获取当前种子? [英] How to get current seed from C++ rand()?

查看:32
本文介绍了如何从 C++ rand() 获取当前种子?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我基于 C++ rand() 函数在我的程序中生成了几千个对象.将它们保留在记忆中将是详尽无遗的.有没有办法在任何给定时间复制 rand() 的当前种子?这将使我有机会仅存储当前的种子而不是完整的对象.(因此我可以通过重新生成完全相同的随机数子序列来重新生成这些对象)

I generate a few thousand object in my program based on the C++ rand() function. Keeping them in the memory would be exhaustive. Is there a way to copy the CURRENT seed of rand() at any given time? This would give me the opportunity to store ONLY the current seeds and not full objects. (thus I could regenerate those objects, by regenerating the exact same sub-sequences of random numbers)

一个详尽的解决方案是存储由 rand() 给出的完整随机数序列 - 不值得.另一个可能是的解决方案是为随机数实现我自己的类.

An exhaustive solution is storing the full sequence of random numbers given by rand() - doesn't worth it. Another would be solution is to implement my own class for randomized numbers.

谷歌没有给我任何积极的线索.讲授rand和srand基础知识的文章有数百篇,我找不到具体的.

Google gave me no positive clues. There are hundreds of articles teaching the basics of rand and srand, and I couldn't find the specific ones.

有谁知道其他带有种子窃取器的随机数生成器吗?

Does anyone know other random number generators with implemented seed-stealer?

感谢您的快速答复!这个问题有更多可能的答案/解决方案,所以我在这里列出了你的答案.

Thank you for your fast answers! There are more possible answers/solutions to this question, so I made a list of your answers here.

解决方案:

  1. 简短的回答是:没有获得种子的标准方法

  1. The short answer is: there is no standard way to get the seed

最接近的可能解决方法是在开始时保存 INITIAL 种子,并计算您调用 rand() 函数的次数.我将此标记为解决方案,因为它适用于每个编译器的当前std::rand()函数(这是主要问题).我对我的 2.0 GHz CPU 进行了基准测试,发现我可以在 35 秒内调用&count rand() 1,000,000,000 次.这听起来不错,但我有 80,000 次调用来生成一个对象.这将世代数限制为 50,000,因为 unsigned long 的大小.无论如何,这是我的代码:

The closest possible workaround is to save the INITIAL seed in the beginning, and count how many times you call the rand() function. I marked this as solution because it works on the current std::rand() function of every compiler (and this was the main question about). I've benchmarked my 2.0 GHz CPU, and found that I can call&count rand() 1,000,000,000 times in 35 seconds. This might sound good, but I have 80,000 calls to generate one object. This restricts the number of generations to 50,000 because the size of unsigned long. Anyway, here is my code:

class rand2
{
   unsigned long n;

   public:

   rand2 () : n(0) {}

   unsigned long rnd()
   {
      n++;
      return rand();
   }
   // get number of rand() calls inside this object
   unsigned long getno ()
   {
      return n;
   }
   // fast forward to a saved position called rec
   void fast_forward (unsigned long rec)
   {
      while (n < rec) rnd();
   }
};

  • 另一种方法是实现您自己的伪随机数生成器,就像 Matteo Italia 建议的那样.这是最快的,也可能是最好的解决方案.您不限于 4,294,967,295 次 rand() 调用,也不需要使用其他库.值得一提的是,不同的编译器有不同的生成器.我在 Mingw/GCC 3.4.2 和 G++ 4.3.2 中比较了 Matteo 的 LCGrand().他们三个都不同(seed = 0).

  • Another way is to implement your own Pseudo-random number generator, like the one Matteo Italia suggested. This is the fastest, and possibly the BEST solution. You're not restricted to 4,294,967,295 rand() calls, and don't need to use other libraries either. It's worth mentioning that different compilers have different generators. I've compared Matteo's LCG with rand() in Mingw/GCC 3.4.2 and G++ 4.3.2. All 3 of them were different (with seed = 0).

    使用 C++11 或其他库中的生成器,如 Cubbi、Jerry Coffin 和 Mike Seymour 建议的那样.如果您已经在与他们合作,这是最好的主意.C++11 生成器的链接:http://en.cppreference.com/w/cpp/数字/随机(这里也有一些算法说明)

    Use generators from C++11 or other libraries as Cubbi, Jerry Coffin and Mike Seymour suggested. This is the best idea, if you're already working with them. Link for C++11 generators: http://en.cppreference.com/w/cpp/numeric/random (there are some algorithm descriptions here too)

    推荐答案

    使用 srand() 设置种子.保存您用作种子的值.

    Use srand() to set the seed. save the value you used as the seed.

    http://cplusplus.com/reference/clibrary/cstdlib/srand/

    这篇关于如何从 C++ rand() 获取当前种子?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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