久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    • <bdo id='auxeW'></bdo><ul id='auxeW'></ul>
      <i id='auxeW'><tr id='auxeW'><dt id='auxeW'><q id='auxeW'><span id='auxeW'><b id='auxeW'><form id='auxeW'><ins id='auxeW'></ins><ul id='auxeW'></ul><sub id='auxeW'></sub></form><legend id='auxeW'></legend><bdo id='auxeW'><pre id='auxeW'><center id='auxeW'></center></pre></bdo></b><th id='auxeW'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='auxeW'><tfoot id='auxeW'></tfoot><dl id='auxeW'><fieldset id='auxeW'></fieldset></dl></div>
      <legend id='auxeW'><style id='auxeW'><dir id='auxeW'><q id='auxeW'></q></dir></style></legend>

        <small id='auxeW'></small><noframes id='auxeW'>

        <tfoot id='auxeW'></tfoot>

        如何生成線程安全的統一隨機數?

        How do I generate thread-safe uniform random numbers?(如何生成線程安全的統一隨機數?)
        <legend id='acqxu'><style id='acqxu'><dir id='acqxu'><q id='acqxu'></q></dir></style></legend>
          <tbody id='acqxu'></tbody>
        <tfoot id='acqxu'></tfoot>

                <small id='acqxu'></small><noframes id='acqxu'>

                • <bdo id='acqxu'></bdo><ul id='acqxu'></ul>
                • <i id='acqxu'><tr id='acqxu'><dt id='acqxu'><q id='acqxu'><span id='acqxu'><b id='acqxu'><form id='acqxu'><ins id='acqxu'></ins><ul id='acqxu'></ul><sub id='acqxu'></sub></form><legend id='acqxu'></legend><bdo id='acqxu'><pre id='acqxu'><center id='acqxu'></center></pre></bdo></b><th id='acqxu'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='acqxu'><tfoot id='acqxu'></tfoot><dl id='acqxu'><fieldset id='acqxu'></fieldset></dl></div>
                  本文介紹了如何生成線程安全的統一隨機數?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我的程序需要在某個范圍內生成許多隨機整數(int min,int max).每次調用都有一個不同的范圍.什么是好的(最好是線程安全的)方法來做到這一點?以下不是線程安全的(并且使用了人們似乎不鼓勵的 rand()):

                  My program needs to generate many random integers in some range (int min, int max). Each call will have a different range. What is a good (preferably thread-safe) way to do this? The following is not thread-safe (and uses rand(), which people seem to discourage):

                  int intRand(const int & min, const int & max)
                  {
                      return (rand() % (max+1-min)) + min;
                  }
                  

                  慢很多,但使用:

                  int intRand(const int & min, const int & max) {
                      std::default_random_engine generator;
                      std::uniform_int_distribution<int> distribution(min,max);
                      return distribution(generator);
                  }
                  

                  這樣的事情就是我想要的(雖然 changeParameters 函數不存在):

                  Something like this is what I'm going for (the changeParameters function doesn't exist though):

                  int intRand(const int & min, const int & max) {
                      static std::default_random_engine generator;
                      static std::uniform_int_distribution<int> distribution(0, 10);
                      distribution.changeParameters(min, max);
                      return distribution(generator);
                  }
                  

                  另一種選擇是在 uniform_int_distribution 上做一個廣泛的范圍,然后像第一個例子一樣使用 mod.但是,我正在做統計工作,所以我希望數字來自盡可能無偏的分布(例如,如果使用的分布范圍不是 (max-min) 的倍數,分布將略微有偏見).這是一個選項,但同樣,我想避免它.

                  Another option would be to make a wide range on the uniform_int_distribution and then use mod like in the first example. However, I'm doing statistical work, so I want the numbers to come from as unbiased of a distribution as possible (e.g., if the range of the distribution used is not a multiple of (max-min), the distribution will be slightly biased). This is an option, but again, I would like to avoid it.

                  解決方案 此解決方案來自 @konrad-rudolph @mark-ransom 和 @mathk 的答案.隨機數生成器的播種是為了滿足我的特定需求.更常見的方法是使用 time(NULL).如果您在同一秒內創建多個線程,那么它們將獲得相同的種子.即使使用 clock() 這也是一個問題,所以我們包含線程 id.一個缺點 - 這會泄漏內存 --- 每個線程一個生成器.

                  SOLUTION This solution comes from the answers by @konrad-rudolph @mark-ransom and @mathk . The seeding of the random number generator is done to suit my particular needs. A more common approach would be to use time(NULL). If you make many threads in the same second, they would then get the same seed though. Even with clock() this is an issue, so we include the thread id. A drawback - this leaks memory --- one generator per thread.

                  #if defined (_MSC_VER)  // Visual studio
                      #define thread_local __declspec( thread )
                  #elif defined (__GCC__) // GCC
                      #define thread_local __thread
                  #endif
                  
                  #include <random>
                  #include <time.h>
                  #include <thread>
                  
                  using namespace std;
                  
                  /* Thread-safe function that returns a random number between min and max (inclusive).
                  This function takes ~142% the time that calling rand() would take. For this extra
                  cost you get a better uniform distribution and thread-safety. */
                  int intRand(const int & min, const int & max) {
                      static thread_local mt19937* generator = nullptr;
                      if (!generator) generator = new mt19937(clock() + this_thread::get_id().hash());
                      uniform_int_distribution<int> distribution(min, max);
                      return distribution(*generator);
                  }
                  

                  推薦答案

                  你試過了嗎?

                  int intRand(const int & min, const int & max) {
                      static thread_local std::mt19937 generator;
                      std::uniform_int_distribution<int> distribution(min,max);
                      return distribution(generator);
                  }
                  

                  分布非常便宜(它們將被優化器完全內聯,因此唯一剩余的開銷是實際的隨機數重新縮放).不要害怕根據需要經常重新生成它們 - 事實上,從概念上講,重置它們并不便宜(這就是為什么不存在該操作的原因).

                  Distributions are extremely cheap (they will be completely inlined by the optimiser so that the only remaining overhead is the actual random number rescaling). Don’t be afraid to regenerate them as often as you need –?in fact, resetting them would conceptually be no cheaper (which is why that operation doesn’t exist).

                  另一方面,實際的隨機數生成器是一個重量級的對象,帶有很多狀態并且需要相當長的時間來構建,因此每個線程(甚至跨線程,但那么你就需要同步訪問,從長遠來看這會更昂貴).

                  The actual random number generator, on the other hand, is a heavy-weight object carrying a lot of state and requiring quite some time to be constructed, so that should only be initialised once per thread (or even across threads, but then you’d need to synchronise access which is more costly in the long run).

                  這篇關于如何生成線程安全的統一隨機數?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  read input files, fastest way possible?(讀取輸入文件,最快的方法?)
                  The easiest way to read formatted input in C++?(在 C++ 中讀取格式化輸入的最簡單方法?)
                  Reading from .txt file into two dimensional array in c++(從 .txt 文件讀取到 C++ 中的二維數組)
                  How to simulate a key press in C++(如何在 C++ 中模擬按鍵按下)
                  Why doesn#39;t getline(cin, var) after cin.ignore() read the first character of the string?(為什么在 cin.ignore() 之后沒有 getline(cin, var) 讀取字符串的第一個字符?)
                  What is the cin analougus of scanf formatted input?(scanf 格式輸入的 cin 類比是什么?)
                  • <bdo id='zf3qx'></bdo><ul id='zf3qx'></ul>
                  • <small id='zf3qx'></small><noframes id='zf3qx'>

                      <tfoot id='zf3qx'></tfoot>
                        <tbody id='zf3qx'></tbody>

                          <i id='zf3qx'><tr id='zf3qx'><dt id='zf3qx'><q id='zf3qx'><span id='zf3qx'><b id='zf3qx'><form id='zf3qx'><ins id='zf3qx'></ins><ul id='zf3qx'></ul><sub id='zf3qx'></sub></form><legend id='zf3qx'></legend><bdo id='zf3qx'><pre id='zf3qx'><center id='zf3qx'></center></pre></bdo></b><th id='zf3qx'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='zf3qx'><tfoot id='zf3qx'></tfoot><dl id='zf3qx'><fieldset id='zf3qx'></fieldset></dl></div>
                            <legend id='zf3qx'><style id='zf3qx'><dir id='zf3qx'><q id='zf3qx'></q></dir></style></legend>

                          1. 主站蜘蛛池模板: 欧美成人激情视频 | 黄色a一级| 免费国产网站 | 日韩精品第一页 | 亚洲高清在线观看 | 久本草精品| 日韩精品久久久久久久酒店 | 日日操夜夜撸 | 秋霞一区二区三区 | 久久久久久网 | 久久综合亚洲 | 日日骚av| 久久噜噜| 伊人av在线 | 中文字幕不卡视频 | 午夜久久久久久 | 国产精品免费一区二区三区 | 在线视频黄 | a毛片大片 | 亚洲精品1区2区 | 一区二区三区国产 | av女优天堂 | 亚洲欧美另类在线 | 久草网站| 六月婷婷在线 | 欧美一道本 | av福利在线观看 | 老司机精品福利视频 | 欧洲黄色网 | 日本在线看片 | 亚洲免费小视频 | 精品一区二区三区免费看 | 美女免费视频网站 | 97色在线| 亚洲福利片 | 色综合天天综合网国产成人网 | 国产精品久久午夜夜伦鲁鲁 | 中文字幕在线免费视频 | 国产精品视频久久 | 欧美成年人视频 | 成人午夜激情视频 |