简体   繁体   English

C++ 随机 uniform_int_distribution 在所有线程中返回相同的值

[英]C++ random uniform_int_distribution return same values in all threads

I have the following code, I need to have a random number in given interval.我有以下代码,我需要在给定的时间间隔内有一个随机数。 Seems to work how I need.似乎按我的需要工作。

   std::default_random_engine eng;
     std::uniform_int_distribution<int> dist(3, 7);
     int timeout = dist(eng);

But then I run it in different threads and repeated in the loop.但后来我在不同的线程中运行它并在循环中重复。

    std::default_random_engine defRandEng(std::this_thread::get_id());
    std::uniform_int_distribution<int> dist(3, 7);
    int timeout; // if I put timeout = dist(defRandEng); here it's all the same

    while (true)
    {
        timeout = dist(defRandEng);
        std::cout<<"Thread "<<std::this_thread::get_id()<<" timeout = "<<timeout<<std::endl;
        std::this_thread::sleep_for(std::chrono::seconds(timeout));
}

But for every iteration in all threads the values are the same但是对于所有线程中的每次迭代,值都是相同的

Thread 139779167999744 timeout = 6
Thread 139779134428928 timeout = 6
Thread 139779067287296 timeout = 6
Thread 139779117643520 timeout = 6
Thread 139779100858112 timeout = 6
Thread 139779084072704 timeout = 6
Thread 139779151214336 timeout = 6
Thread 139779050501888 timeout = 6
Thread 139779033716480 timeout = 6

next interation下一次互动

Thread 139779167999744 timeout = 4
Thread 139779151214336 timeout = 4
Thread 139779134428928 timeout = 4
Thread 139779117643520 timeout = 4
Thread 139779100858112 timeout = 4
Thread 139779084072704 timeout = 4
Thread 139779067287296 timeout = 4
Thread 139779050501888 timeout = 4
Thread 139779033716480 timeout = 4

You need to provide a seed based on some naturally random value to your random engine.您需要为随机引擎提供基于一些自然随机值的种子。 The below example, that was adopted from your code snippets, works fine with 3 threads:下面的示例是从您的代码片段中采用的,适用于 3 个线程:

std::mutex lock;

void sample_time_out()
{
   std::stringstream ss;
   ss << std::this_thread::get_id();
   uint64_t thread_id = std::stoull(ss.str());

   std::default_random_engine eng(thread_id);
   std::uniform_int_distribution<int> dis(3, 7);


   for (int i = 0; i < 3; i++)
   {
      auto timeout = dis(eng);

      std::this_thread::sleep_for(std::chrono::seconds(timeout));
      {
         std::unique_lock<std::mutex> lock1(lock);
         std::cout << "Thread " << std::this_thread::get_id() << " timeout = " << timeout << std::endl;
      }
   }
}

int main()
{
   std::thread t1(sample_time_out);
   std::thread t2(sample_time_out);
   std::thread t3(sample_time_out);

   t1.join();
   t2.join();
   t3.join();

   return 0;
}

And the output of my first run is:我第一次运行的 output 是:

Thread 31420 timeout = 3
Thread 18616 timeout = 6
Thread 31556 timeout = 7
Thread 31420 timeout = 4
Thread 18616 timeout = 7
Thread 31420 timeout = 6
Thread 31556 timeout = 7
Thread 18616 timeout = 4
Thread 31556 timeout = 7

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

相关问题 C++ 从函数返回 (uniform_int_distribution) - C++ Return (uniform_int_distribution) from a function C ++数值库:std :: uniform_int_distribution &lt;&gt;,更改两次调用之间的分配范围 - C++ numerics lib: std::uniform_int_distribution<>, change bounds of distribution between calls 为什么是 <random> 每次使用std :: uniform_int_distribution时,库都会产生相同的结果 - Why is <random> library producing the same results every time when using std::uniform_int_distribution 统一分配问题 - Problems with uniform_int_distribution 是否可以为boost :: random :: uniform_int_distribution &lt;&gt;设置确定性种子? - Is it possible to set a deterministic seed for boost::random::uniform_int_distribution<>? Visual Studio 2019 c++latest random_device uniform_int_distribution 抛出未处理的异常 - Visual Studio 2019 c++latest random_device uniform_int_distribution throws unhandled exception 使自己的随机数类与uniform_int_distribution兼容 - Making own random number class compitible with uniform_int_distribution 使用 c++11 std::uniform_int_distribution 生成随机素数 - Generate a random prime using c++11 std::uniform_int_distribution std::uniform_int_distribution 不够随机 - std::uniform_int_distribution isn't random enough C++uniform_int_distribution 在第一次调用时总是返回 min() - C++ uniform_int_distribution always returning min() on first invocation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM