pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    • <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="iawkoic" 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>

        如何生成線程安全的統(tǒng)一隨機(jī)數(shù)?

        How do I generate thread-safe uniform random numbers?(如何生成線程安全的統(tǒng)一隨機(jī)數(shù)?)
        <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="u22iq2q" id='acqxu'><tfoot id='acqxu'></tfoot><dl id='acqxu'><fieldset id='acqxu'></fieldset></dl></div>
                  本文介紹了如何生成線程安全的統(tǒng)一隨機(jī)數(shù)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我的程序需要在某個范圍內(nèi)生成許多隨機(jī)整數(shù)(int min,int max).每次調(diào)用都有一個不同的范圍.什么是好的(最好是線程安全的)方法來做到這一點(diǎn)?以下不是線程安全的(并且使用了人們似乎不鼓勵的 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 函數(shù)不存在):

                  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.但是,我正在做統(tǒng)計(jì)工作,所以我希望數(shù)字來自盡可能無偏的分布(例如,如果使用的分布范圍不是 (max-min) 的倍數(shù),分布將略微有偏見).這是一個選項(xiàng),但同樣,我想避免它.

                  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 的答案.隨機(jī)數(shù)生成器的播種是為了滿足我的特定需求.更常見的方法是使用 time(NULL).如果您在同一秒內(nèi)創(chuàng)建多個線程,那么它們將獲得相同的種子.即使使用 clock() 這也是一個問題,所以我們包含線程 id.一個缺點(diǎn) - 這會泄漏內(nèi)存 --- 每個線程一個生成器.

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

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

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

                  另一方面,實(shí)際的隨機(jī)數(shù)生成器是一個重量級的對象,帶有很多狀態(tài)并且需要相當(dāng)長的時間來構(gòu)建,因此每個線程(甚至跨線程,但那么你就需要同步訪問,從長遠(yuǎn)來看這會更昂貴).

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

                  這篇關(guān)于如何生成線程安全的統(tǒng)一隨機(jī)數(shù)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  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++ 中的二維數(shù)組)
                  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="ss0kkcm" 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. 主站蜘蛛池模板: 今日娱乐圈——影视剧集_八卦娱乐_明星八卦_最新娱乐八卦新闻 | 兰州牛肉面加盟,兰州牛肉拉面加盟-京穆兰牛肉面 | 定制异形重型钢格栅板/钢格板_定做踏步板/排水沟盖板_钢格栅板批发厂家-河北圣墨金属制品有限公司 | 东亚液氮罐-液氮生物容器-乐山市东亚机电工贸有限公司 | 欧版反击式破碎机-欧版反击破-矿山石料破碎生产线-青州奥凯诺机械 | 粉末冶金-粉末冶金齿轮-粉末冶金零件厂家-东莞市正朗精密金属零件有限公司 | 振动台-振动试验台-振动冲击台-广东剑乔试验设备有限公司 | 西安中国国际旅行社(西安国旅) | 应急灯_消防应急灯_应急照明灯_应急灯厂家-大成智慧官网 | 实验室pH计|电导率仪|溶解氧测定仪|离子浓度计|多参数水质分析仪|pH电极-上海般特仪器有限公司 | 全温恒温摇床-水浴气浴恒温摇床-光照恒温培养摇床-常州金坛精达仪器制造有限公司 | 电气控制系统集成商-PLC控制柜变频控制柜-非标自动化定制-电气控制柜成套-NIDEC CT变频器-威肯自动化控制 | 快干水泥|桥梁伸缩缝止水胶|伸缩缝装置生产厂家-广东广航交通科技有限公司 | 达利园物流科技集团-| 钢制拖链生产厂家-全封闭钢制拖链-能源钢铝拖链-工程塑料拖链-河北汉洋机械制造有限公司 | 包装机传感器-搅拌站传感器-山东称重传感器厂家-济南泰钦电气 | 防爆暖风机_防爆电暖器_防爆电暖风机_防爆电热油汀_南阳市中通智能科技集团有限公司 | 亿诺千企网-企业核心产品贸易 | 压砖机、液压制砖机、静压砖机、环保砖机生产厂家—杜甫机械 | 十字轴_十字轴万向节_十字轴总成-南京万传机械有限公司 | 深圳彩钢板_彩钢瓦_岩棉板_夹芯板_防火复合彩钢板_长鑫 | 济南侦探调查-济南调查取证-山东私家侦探-山东白豹调查咨询公司 密集架|电动密集架|移动密集架|黑龙江档案密集架-大量现货厂家销售 | 陕西安玻璃自动感应门-自动重叠门-磁悬浮平开门厂家【捷申达门业】 | 软文发布-新闻发布推广平台-代写文章-网络广告营销-自助发稿公司媒介星 | 中原网视台| 旋振筛|圆形摇摆筛|直线振动筛|滚筒筛|压榨机|河南天众机械设备有限公司 | 金环宇|金环宇电线|金环宇电缆|金环宇电线电缆|深圳市金环宇电线电缆有限公司|金环宇电缆集团 | Maneurop/美优乐压缩机,活塞压缩机,型号规格,技术参数,尺寸图片,价格经销商 | 长沙网站建设制作「网站优化推广」-网页设计公司-速马科技官网 | 集菌仪厂家_全封闭_封闭式_智能智能集菌仪厂家-上海郓曹 | 智慧旅游_智慧景区_微景通-智慧旅游景区解决方案提供商 | 武汉天安盾电子设备有限公司 - 安盾安检,武汉安检门,武汉安检机,武汉金属探测器,武汉测温安检门,武汉X光行李安检机,武汉防爆罐,武汉车底安全检查,武汉液体探测仪,武汉安检防爆设备 | 杭州高温泵_热水泵_高温油泵|昆山奥兰克泵业制造有限公司 | 北京网站建设首页,做网站选【优站网】,专注北京网站建设,北京网站推广,天津网站建设,天津网站推广,小程序,手机APP的开发。 | 耐火浇注料-喷涂料-浇注料生产厂家_郑州市元领耐火材料有限公司 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | 井式炉-台车式回火炉-丹阳市电炉厂有限公司 | 砂石生产线_石料生产线设备_制砂生产线设备价格_生产厂家-河南中誉鼎力智能装备有限公司 | 棉柔巾代加工_洗脸巾oem_一次性毛巾_浴巾生产厂家-杭州禾壹卫品科技有限公司 | 哈希余氯测定仪,分光光度计,ph在线监测仪,浊度测定仪,试剂-上海京灿精密机械有限公司 | 网站建设-网站制作-网站设计-网站开发定制公司-网站SEO优化推广-咏熠软件 | 烟雾净化器-滤筒除尘器-防爆除尘器-除尘器厂家-东莞执信环保科技有限公司 |