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

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

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

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

      1. <legend id='ZZjzg'><style id='ZZjzg'><dir id='ZZjzg'><q id='ZZjzg'></q></dir></style></legend>

      2. 在一個(gè)范圍內(nèi)生成無(wú)偏隨機(jī)整數(shù)的最佳算法是什

        What is the optimal algorithm for generating an unbiased random integer within a range?(在一個(gè)范圍內(nèi)生成無(wú)偏隨機(jī)整數(shù)的最佳算法是什么?)
          <tbody id='CGvgF'></tbody>

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

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

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

            1. <tfoot id='CGvgF'></tfoot>
                • 本文介紹了在一個(gè)范圍內(nèi)生成無(wú)偏隨機(jī)整數(shù)的最佳算法是什么?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

                  問(wèn)題描述

                  在這個(gè) StackOverflow 問(wèn)題中:

                  In this StackOverflow question:

                  從范圍生成隨機(jī)整數(shù)

                  接受的答案建議使用以下公式在給定的 minmax 之間生成一個(gè)隨機(jī)整數(shù),其中 minmax 被包含在范圍內(nèi):

                  the accepted answer suggests the following formula for generating a random integer in between given min and max, with min and max being included into the range:

                  output = min + (rand() % (int)(max - min + 1))
                  

                  但它也說(shuō)

                  這仍然略微偏向于較低的數(shù)字......它也是可以擴(kuò)展它以消除偏差.

                  This is still slightly biased towards lower numbers ... It's also possible to extend it so that it removes the bias.

                  但它沒(méi)有解釋為什么它偏向于較低的數(shù)字或如何消除這種偏向.所以,問(wèn)題是:這是在(有符號(hào))范圍內(nèi)生成隨機(jī)整數(shù)的最佳方法,而不依賴于任何花哨的東西,只是 rand() 函數(shù),如果它是最優(yōu),如何消除偏差?

                  But it doesn't explain why it's biased towards lower numbers or how to remove the bias. So, the question is: is this the most optimal approach to generation of a random integer within a (signed) range while not relying on anything fancy, just rand() function, and in case if it is optimal, how to remove the bias?

                  我剛剛針對(duì)浮點(diǎn)外推測(cè)試了@Joey 建議的 while 循環(huán)算法:

                  I've just tested the while-loop algorithm suggested by @Joey against floating-point extrapolation:

                  static const double s_invRandMax = 1.0/((double)RAND_MAX + 1.0);
                  return min + (int)(((double)(max + 1 - min))*rand()*s_invRandMax);
                  

                  查看有多少均勻的球"落"入并分布在多個(gè)桶"中,一個(gè)測(cè)試用于浮點(diǎn)外推,另一個(gè)用于 while 循環(huán)算法.但結(jié)果證明結(jié)果會(huì)因球"(和桶")的數(shù)量而異,因此我無(wú)法輕易選出獲勝者.可以在 此 Ideone 頁(yè)面 中找到工作代碼.例如,對(duì)于 10 個(gè)桶和 100 個(gè)球,浮點(diǎn)外推的桶之間理想概率的最大偏差小于 while 循環(huán)算法(分別為 0.04 和 0.05)但有 1000 個(gè)球,while-loop 算法的最大偏差較小(0.024 和 0.011),并且在 10000 個(gè)球的情況下,浮點(diǎn)外推再次做得更好(0.0034 和 0.0053),依此類推.的一致性.考慮到?jīng)]有一種算法始終產(chǎn)生比其他算法更好的均勻分布的可能性,讓我傾向于浮點(diǎn)外推,因?yàn)樗坪醣?while 循環(huán)算法執(zhí)行得更快.那么選擇浮點(diǎn)外推算法好還是我的測(cè)試/結(jié)論不完全正確?

                  to see how much uniformly "balls" are "falling" into and are being distributed among a number of "buckets", one test for the floating-point extrapolation and another for the while-loop algorithm. But results turned out to be varying depending on the number of "balls" (and "buckets") so I couldn't easily pick a winner. The working code can be found at this Ideone page. For example, with 10 buckets and 100 balls the maximum deviation from the ideal probability among buckets is less for the floating-point extrapolation than for the while-loop algorithm (0.04 and 0.05 respectively) but with 1000 balls, the maximum deviation of the while-loop algorithm is lesser (0.024 and 0.011), and with 10000 balls, the floating-point extrapolation is again doing better (0.0034 and 0.0053), and so on without much of consistency. Thinking of the possibility that none of the algorithms consistently produces uniform distribution better than that of the other algorithm, makes me lean towards the floating-point extrapolation since it appears to perform faster than the while-loop algorithm. So is it fine to choose the floating-point extrapolation algorithm or my testings/conclusions are not completely correct?

                  推薦答案

                  當(dāng)隨機(jī)數(shù)生成器的輸出個(gè)數(shù) (RAND_MAX+1) 不能被所需的范圍 (max-min+1) 整除時(shí)出現(xiàn)問(wèn)題.由于從隨機(jī)數(shù)到輸出會(huì)有一致的映射,因此某些輸出將映射到比其他輸出更多的隨機(jī)數(shù).這與映射是如何完成的無(wú)關(guān)——您可以使用模數(shù)、除法、轉(zhuǎn)換為浮點(diǎn)數(shù),無(wú)論您能想出什么伏都教,基本問(wèn)題仍然存在.

                  The problem occurs when the number of outputs from the random number generator (RAND_MAX+1) is not evenly divisible by the desired range (max-min+1). Since there will be a consistent mapping from a random number to an output, some outputs will be mapped to more random numbers than others. This is regardless of how the mapping is done - you can use modulo, division, conversion to floating point, whatever voodoo you can come up with, the basic problem remains.

                  問(wèn)題的嚴(yán)重性非常小,要求不高的應(yīng)用程序通??梢院雎运?范圍越小,RAND_MAX越大,效果越不明顯.

                  The magnitude of the problem is very small, and undemanding applications can generally get away with ignoring it. The smaller the range and the larger RAND_MAX is, the less pronounced the effect will be.

                  我采用了您的示例程序并對(duì)其進(jìn)行了一些調(diào)整.首先我創(chuàng)建了一個(gè)特殊版本的rand,范圍只有0-255,以更好地展示效果.我對(duì) rangeRandomAlg2 做了一些調(diào)整.最后我將球"的數(shù)量改為 1000000 以提高一致性.您可以在此處查看結(jié)果:http://ideone.com/4P4HY

                  I took your example program and tweaked it a bit. First I created a special version of rand that only has a range of 0-255, to better demonstrate the effect. I made a few tweaks to rangeRandomAlg2. Finally I changed the number of "balls" to 1000000 to improve the consistency. You can see the results here: http://ideone.com/4P4HY

                  請(qǐng)注意,浮點(diǎn)版本產(chǎn)生兩個(gè)緊密分組的概率,接近 0.101 或 0.097,介于兩者之間.這就是行動(dòng)中的偏見(jiàn).

                  Notice that the floating-point version produces two tightly grouped probabilities, near either 0.101 or 0.097, nothing in between. This is the bias in action.

                  我認(rèn)為稱其為Java 的算法"有點(diǎn)誤導(dǎo) - 我確信它比 Java 古老得多.

                  I think calling this "Java's algorithm" is a bit misleading - I'm sure it's much older than Java.

                  int rangeRandomAlg2 (int min, int max)
                  {
                      int n = max - min + 1;
                      int remainder = RAND_MAX % n;
                      int x;
                      do
                      {
                          x = rand();
                      } while (x >= RAND_MAX - remainder);
                      return min + x % n;
                  }
                  

                  這篇關(guān)于在一個(gè)范圍內(nèi)生成無(wú)偏隨機(jī)整數(shù)的最佳算法是什么?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

                  相關(guān)文檔推薦

                  read input files, fastest way possible?(讀取輸入文件,最快的方法?)
                  The easiest way to read formatted input in C++?(在 C++ 中讀取格式化輸入的最簡(jiǎn)單方法?)
                  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() 之后沒(méi)有 getline(cin, var) 讀取字符串的第一個(gè)字符?)
                  What is the cin analougus of scanf formatted input?(scanf 格式輸入的 cin 類比是什么?)

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

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

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

                        • <tfoot id='bfXu7'></tfoot>

                          1. 主站蜘蛛池模板: 日韩高清av | 欧美日韩网站 | 久久在线| 成人在线黄色 | www久久久 | 欧美激情a∨在线视频播放 成人免费共享视频 | 欧美一区二区在线免费观看 | 久久久久久成人网 | 欧美日韩视频在线第一区 | 亚洲永久精品国产 | 欧美色性| 久久99精品久久久 | 91高清免费观看 | 成人影院网站ww555久久精品 | 亚洲国产aⅴ成人精品无吗 欧美激情欧美激情在线五月 | 国产一区高清 | 精品一区二区三区在线观看国产 | 亚洲精品视频观看 | 一区二区福利视频 | 久久狠狠| 国产专区视频 | 日韩网| h视频亚洲| 成人精品免费视频 | 国产99视频精品免费播放照片 | 视频一区二区三区中文字幕 | 国产视频第一页 | 国产福利91精品 | 欧美一区免费 | 日韩免费视频一区二区 | 国产成人av在线 | 在线看亚洲 | 欧美a级成人淫片免费看 | 欧美国产视频 | 欧美成人在线网站 | 中文字幕第一页在线 | 亚洲视频一 | 黄a免费看 | 综合伊人 | 国产一级在线 | 欧美日韩亚洲成人 |