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

<legend id='SwGYy'><style id='SwGYy'><dir id='SwGYy'><q id='SwGYy'></q></dir></style></legend>

    • <bdo id='SwGYy'></bdo><ul id='SwGYy'></ul>

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

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

      1. C++uniform_int_distribution 在第一次調用時總是返回

        C++ uniform_int_distribution always returning min() on first invocation(C++uniform_int_distribution 在第一次調用時總是返回 min())

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

          <tfoot id='oERXe'></tfoot>

              <tbody id='oERXe'></tbody>

          1. <i id='oERXe'><tr id='oERXe'><dt id='oERXe'><q id='oERXe'><span id='oERXe'><b id='oERXe'><form id='oERXe'><ins id='oERXe'></ins><ul id='oERXe'></ul><sub id='oERXe'></sub></form><legend id='oERXe'></legend><bdo id='oERXe'><pre id='oERXe'><center id='oERXe'></center></pre></bdo></b><th id='oERXe'></th></span></q></dt></tr></i><div class="jnrcn2e" id='oERXe'><tfoot id='oERXe'></tfoot><dl id='oERXe'><fieldset id='oERXe'></fieldset></dl></div>

              • <bdo id='oERXe'></bdo><ul id='oERXe'></ul>
                <legend id='oERXe'><style id='oERXe'><dir id='oERXe'><q id='oERXe'></q></dir></style></legend>
                1. 本文介紹了C++uniform_int_distribution 在第一次調用時總是返回 min()的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  在標準庫的至少一個實現中,std::uniform_int_distribution<> 的第一次調用不會返回隨機值,而是分布的最小值.也就是說,給定代碼:

                  In at least one implementation of the standard library, the first invocation of a std::uniform_int_distribution<> does not return a random value, but rather the distribution's min value. That is, given the code:

                  default_random_engine engine( any_seed() );
                  uniform_int_distribution< int > distribution( smaller, larger );
                  auto x = distribution( engine );
                  assert( x == smaller );
                  

                  對于 any_seed()smallersmaller 的任何值,

                  ...x 實際上將 smaller,或更大.

                  ...x will in fact be smaller for any values of any_seed(), smaller, or larger.

                  要在家玩,您可以嘗試在 gcc 4.8.1 中演示此問題的代碼示例.

                  To play along at home, you can try a code sample that demonstrates this problem in gcc 4.8.1.

                  我相信這是正確的行為?如果這是正確的行為,為什么隨機分布會返回這個明顯非隨機的值?

                  I trust this is not correct behavior? If it is correct behavior, why would a random distribution return this clearly non-random value?

                  推薦答案

                  對觀察到的行為的解釋

                  如果可能結果的范圍小于 rng 產生的數字范圍,uniform_int_distribution 就是這樣將隨機位映射到數字的:

                  Explanation for the observed behavior

                  This is how uniform_int_distribution maps the random bits to numbers if the range of possible outcomes is smaller than the range of number the rng produces:

                  const __uctype __uerange = __urange + 1; // __urange can be zero
                  const __uctype __scaling = __urngrange / __uerange;
                  const __uctype __past = __uerange * __scaling;
                  do
                    __ret = __uctype(__urng()) - __urngmin;
                  while (__ret >= __past);
                  __ret /= __scaling;
                  

                  其中 __urangelarger -smaller 并且 __urngrange 是 rng 可以返回的最大值和最小值之間的差值.(代碼來自 libstdc++ 6.1 中的 bits/uniform_int_dist.h)

                  where __urange is larger - smaller and __urngrange is the difference between the maximum and the minimum value the rng can return. (Code from bits/uniform_int_dist.h in libstdc++ 6.1)

                  在我們的例子中,rng default_random_engine 是一個 minstd_rand0,它產生 __scaling == 195225785 對于范圍 [0,10] 你測試.因此,如果 rng() <195225785,分配將返回0.

                  In our case, the rng default_random_engine is a minstd_rand0, which yields __scaling == 195225785 for the range [0,10] you tested with. Thus, if rng() < 195225785, the distribution will return 0.

                  minstd_rand0 返回的第一個數字是

                  (16807 * seed) % 2147483647
                  

                  (其中 seed == 0 被調整為 1 順便說一句).因此,我們可以看到由 minstd_rand0 產生的第一個值以小于 11615 的數字作為種子將產生 0,uniform_int_distribution<國際 >分布( 0, 10 ); 你用過.(修改我的一個錯誤.;))

                  (where seed == 0 gets adjusted to 1 btw). We can thus see that the first value produced by a minstd_rand0 seeded with a number smaller than 11615 will yield 0 with the uniform_int_distribution< int > distribution( 0, 10 ); you used. (mod off-by-one-errors on my part. ;) )

                  您提到了更大種子的問題會消失:一旦種子變得足夠大以實際使 mod 操作執行某些操作,我們就不能簡單地通過除法將整個范圍的值分配給相同的輸出,因此結果將看起來更好.

                  You mentioned the problem going away for bigger seeds: As soon as the seeds get big enough to actually make the mod operation do something, we cannot simply assign a whole range of values to the same output by division, so the results will look better.

                  沒有.通過始終選擇較小的隨機數,您在應該是隨機的 32 位種子中引入了顯著的偏差.結果中出現的偏見并不奇怪或邪惡.對于隨機種子,即使您的 minstd_rand0 也會產生相當均勻的隨機第一個值.(雖然之后的數字序列不會有很好的統計質量.)

                  No. You introduced significant bias in what is supposed to be a random 32 bit seed by always choosing it small. That bias showing up in the results is not surprising or evil. For random seeds, even your minstd_rand0 will yield a fairly uniformly random first value. (Though the sequence of numbers after that will not be of great statistical quality.)

                  案例 1:您想要高統計質量的隨機數.

                  Case 1: You want random number of high statistical quality.

                  為此,您可以使用更好的 rng,例如 mt19937 并為其 整個 狀態空間設定種子.對于 Mersenne Twister,這是 624 個 32 位整數.(作為參考,這里是我嘗試正確執行此操作的一些有用建議在答案中.)

                  For that, you use a better rng like mt19937 and seed its entire state space. For the Mersenne Twister, that's 624 32-bit integers. (For reference, here is my attempt to do this properly with some helpful suggestions in the answer.)

                  案例 2:您真的只想使用那些小種子.

                  Case 2: You really want to use those small seeds only.

                  我們仍然可以從中獲得不錯的結果.問題是偽隨機數生成器通常有點連續地"依賴于隨機數生成器.在他們的種子上.為了解決這個問題,我們丟棄了足夠的數字,讓最初相似的輸出序列發散.因此,如果您的種子必須很小,您可以像這樣初始化您的 rng:

                  We can still get decent results out of this. The problem is that pseudo random number generators commonly depend "somewhat continuously" on their seed. To ship around this, we discard enough numbers to let the initially similar sequences of output diverge. So if your seed must be small, you can initialize your rng like this:

                  std::mt19937 rng(smallSeed);
                  rng.discard(700000);
                  

                  為此使用像 Mersenne Twister 這樣的好 rng 至關重要.我不知道有什么方法可以從種子不佳的 minstd_rand0 中獲得合適的值,例如參見 這個火車失事.即使播種正確,mt19937 的統計特性也遠勝一籌.

                  It is vital that you use a good rng like the Mersenne Twister for this. I do not know of any method to get even decent values out of a poorly seeded minstd_rand0, for example see this train-wreck. Even if seeded properly, the statistical properties of a mt19937 are superior by far.

                  您有時會聽到對大型狀態空間或緩慢生成的擔憂,但在嵌入式世界之外通常并不擔心.根據 boost 和 cacert.at,MT 甚至比 minstd_rand0.

                  Concerns about the large state space or slow generation you sometimes hear about are usually of no concern outside the embedded world. According to boost and cacert.at, the MT is even way faster than minstd_rand0.

                  盡管如此,您仍然需要執行丟棄技巧,即使您的結果在沒有肉眼的情況下看起來不錯.在我的系統上它只需要不到一毫秒,而且你不經常播種 rng,所以沒有理由不這樣做.

                  You still need to do the discard trick though, even if your results look good to the naked eye without. It takes less than a millisecond on my system, and you don't seed rngs very often, so there is no reason not to.

                  請注意,我無法準確估計我們需要的丟棄次數,我從 中獲取了該值這個答案,它鏈接這篇論文為理性.我現在沒有時間解決這個問題.

                  Note that I am not able to give you a sharp estimate for the number of discards we need, I took that value from this answer, it links this paper for a rational. I don't have the time to work through that right now.

                  這篇關于C++uniform_int_distribution 在第一次調用時總是返回 min()的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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='arGcO'></bdo><ul id='arGcO'></ul>

                    1. <small id='arGcO'></small><noframes id='arGcO'>

                    2. <tfoot id='arGcO'></tfoot>

                        <tbody id='arGcO'></tbody>

                            <i id='arGcO'><tr id='arGcO'><dt id='arGcO'><q id='arGcO'><span id='arGcO'><b id='arGcO'><form id='arGcO'><ins id='arGcO'></ins><ul id='arGcO'></ul><sub id='arGcO'></sub></form><legend id='arGcO'></legend><bdo id='arGcO'><pre id='arGcO'><center id='arGcO'></center></pre></bdo></b><th id='arGcO'></th></span></q></dt></tr></i><div class="tehgzw7" id='arGcO'><tfoot id='arGcO'></tfoot><dl id='arGcO'><fieldset id='arGcO'></fieldset></dl></div>
                            <legend id='arGcO'><style id='arGcO'><dir id='arGcO'><q id='arGcO'></q></dir></style></legend>
                            主站蜘蛛池模板: 塑料熔指仪-塑料熔融指数仪-熔体流动速率试验机-广东宏拓仪器科技有限公司 | 暴风影音| 百度爱采购运营研究社社群-店铺托管-爱采购代运营-良言多米网络公司 | 乳化沥青设备_改性沥青设备_沥青加温罐_德州市昊通路桥工程有限公司 | 葡萄酒灌装机-食用油灌装机-液体肥灌装设备厂家_青州惠联灌装机械 | 福建成考网-福建成人高考网 | 手持式线材张力计-套帽式风量罩-深圳市欧亚精密仪器有限公司 | 新疆乌鲁木齐网站建设-乌鲁木齐网站制作设计-新疆远璨网络 | 楼梯定制_楼梯设计施工厂家_楼梯扶手安装制作-北京凌步楼梯 | 多功能干燥机,过滤洗涤干燥三合一设备-无锡市张华医药设备有限公司 | 三效蒸发器_多效蒸发器价格_四效三效蒸发器厂家-青岛康景辉 | 展厅设计-展馆设计-专业企业展厅展馆设计公司-昆明华文创意 | 工业冷却塔维修厂家_方形不锈钢工业凉水塔维修改造方案-广东康明节能空调有限公司 | 山东齐鲁漆业有限公司【官网】-工业漆专业生产厂家 | 昆明网络公司|云南网络公司|昆明网站建设公司|昆明网页设计|云南网站制作|新媒体运营公司|APP开发|小程序研发|尽在昆明奥远科技有限公司 | 亮化工程,亮化设计,城市亮化工程,亮化资质合作,长沙亮化照明,杰奥思【官网】 | 上海律师咨询_上海法律在线咨询免费_找对口律师上策法网-策法网 广东高华家具-公寓床|学生宿舍双层铁床厂家【质保十年】 | 元拓建材集团官方网站 | 东莞爱加真空科技有限公司-进口真空镀膜机|真空镀膜设备|Polycold维修厂家 | 口信网(kousing.com) - 行业资讯_行业展会_行业培训_行业资料 | 医用酒精_84消毒液_碘伏消毒液等医用消毒液-漓峰消毒官网 | 高温热泵烘干机,高温烘干热泵,热水设备机组_正旭热泵 | 顶呱呱交易平台-行业领先的公司资产交易服务平台 | 电磁流量计厂家_涡街流量计厂家_热式气体流量计-青天伟业仪器仪表有限公司 | 蜘蛛车-高空作业平台-升降机-高空作业车租赁-臂式伸缩臂叉装车-登高车出租厂家 - 普雷斯特机械设备(北京)有限公司 | BOE画框屏-触摸一体机-触控查询一体机-触摸屏一体机价格-厂家直销-触发电子 | 校园文化空间设计-数字化|中医文化空间设计-党建|法治廉政主题文化空间施工-山东锐尚文化传播公司 | 高精度-恒温冷水机-螺杆式冰水机-蒸发冷冷水机-北京蓝海神骏科技有限公司 | 微量水分测定仪_厂家_卡尔费休微量水分测定仪-淄博库仑 | 集菌仪厂家_全封闭_封闭式_智能智能集菌仪厂家-上海郓曹 | 新材料分散-高速均质搅拌机-超声波分散混合-上海化烁智能设备有限公司 | 南京交通事故律师-专打交通事故的南京律师 | 热风机_工业热风机生产厂家上海冠顶公司提供专业热风机图片价格实惠 | 丝杆升降机-不锈钢丝杆升降机-非标定制丝杆升降机厂家-山东鑫光减速机有限公司 | 焊接烟尘净化器__焊烟除尘设备_打磨工作台_喷漆废气治理设备 -催化燃烧设备 _天津路博蓝天环保科技有限公司 | NM-02立式吸污机_ZHCS-02软轴刷_二合一吸刷软轴刷-厦门地坤科技有限公司 | 北京西风东韵品牌与包装设计公司,创造视觉销售力! | 飞飞影视_热门电影在线观看_影视大全 | 压力控制器,差压控制器,温度控制器,防爆压力控制器,防爆温度控制器,防爆差压控制器-常州天利智能控制股份有限公司 | 卫生纸复卷机|抽纸机|卫生纸加工设备|做卫生纸机器|小型卫生纸加工需要什么设备|卫生纸机器设备多少钱一台|许昌恒源纸品机械有限公司 | 飞飞影视_热门电影在线观看_影视大全 |