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

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

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

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

        <bdo id='ZZtH7'></bdo><ul id='ZZtH7'></ul>

        <tfoot id='ZZtH7'></tfoot>

        在 [0..n-1] 范圍內(nèi)生成 m 個不同的隨機數(shù)

        Generating m distinct random numbers in the range [0..n-1](在 [0..n-1] 范圍內(nèi)生成 m 個不同的隨機數(shù))

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

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

                  <tbody id='N6cfD'></tbody>

                • 本文介紹了在 [0..n-1] 范圍內(nèi)生成 m 個不同的隨機數(shù)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我有兩種方法可以在 [0..n-1] 范圍內(nèi)生成 m 個不同的隨機數(shù)

                  I have two methods of generating m distinct random numbers in the range [0..n-1]

                  方法一:

                  //C++-ish pseudocode
                  int result[m];
                  for(i = 0; i < m; ++i)
                  {
                     int r;
                     do
                     {
                        r = rand()%n;
                     }while(r is found in result array at indices from 0 to i)
                     result[i] = r;   
                  }
                  

                  方法二:

                  //C++-ish pseudocode
                  int arr[n];
                  for(int i = 0; i < n; ++i)
                      arr[i] = i;
                  random_shuffle(arr, arr+n);
                  result = first m elements in arr;
                  

                  當(dāng) n 遠大于 m 時,第一種方法更有效,否則第二種方法更有效.但是更大"不是一個嚴格的概念,是嗎?:)

                  The first method is more efficient when n is much larger than m, whereas the second is more efficient otherwise. But "much larger" isn't that strict a notion, is it? :)

                  問題: 我應(yīng)該使用 n 和 m 的什么公式來確定方法 1 還是方法 2 更有效?(就運行時間的數(shù)學(xué)期望而言)

                  Question: What formula of n and m should I use to determine whether method1 or method2 will be more efficient? (in terms of mathematical expectation of the running time)

                  推薦答案

                  純數(shù)學(xué):
                  讓我們計算兩種情況下 rand() 函數(shù)調(diào)用的數(shù)量并比較結(jié)果:

                  Pure mathematics:
                  Let's calculate the quantity of rand() function calls in both cases and compare the results:

                  案例 1:當(dāng)您已經(jīng)選擇了 k 個數(shù)字時,讓我們看看在步驟 i = k 上調(diào)用的數(shù)學(xué)期望.通過一次 rand() 調(diào)用獲得一個數(shù)字的概率等于 p = (n-k)/n.我們需要知道此類調(diào)用數(shù)量的數(shù)學(xué)期望,這導(dǎo)致獲得我們還沒有的數(shù)字.

                  Case 1: let's see the mathematical expectation of calls on step i = k, when you already have k numbers chosen. The probability to get a number with one rand() call is equal to p = (n-k)/n. We need to know the mathematical expectation of such calls quantity which leads to obtaining a number we don't have yet.

                  使用1 調(diào)用獲得它的概率是p.使用 2 調(diào)用 - q * p,其中 q = 1 - p.在一般情況下,在 n 次調(diào)用之后準確獲得它的概率是 (q^(n-1))*p.因此,數(shù)學(xué)期望為
                  Sum[ n * q^(n-1) * p ], n = 1 -->INF.這個總和等于 1/p(由 wolfram alpha 證明).

                  The probability to get it using 1 call is p. Using 2 calls - q * p, where q = 1 - p. In general case, the probability to get it exactly after n calls is (q^(n-1))*p. Thus, the mathematical expectation is
                  Sum[ n * q^(n-1) * p ], n = 1 --> INF. This sum is equal to 1/p (proved by wolfram alpha).

                  因此,在步驟 i = k 上,您將執(zhí)行 1/p = n/(nk) 調(diào)用 rand()功能.

                  So, on the step i = k you will perform 1/p = n/(n-k) calls of the rand() function.

                  現(xiàn)在讓我們總結(jié)一下:

                  Sum[ n/(n - k) ], k = 0 -->m - 1 = n * T - 方法 1 中的 rand 調(diào)用次數(shù).
                  這里 T = Sum[ 1/(n - k) ], k = 0 -->m - 1

                  Sum[ n/(n - k) ], k = 0 --> m - 1 = n * T - the number of rand calls in method 1.
                  Here T = Sum[ 1/(n - k) ], k = 0 --> m - 1

                  情況 2:

                  這里 rand()random_shuffle 內(nèi)被調(diào)用 n - 1 次(在大多數(shù)實現(xiàn)中).

                  Here rand() is called inside random_shuffle n - 1 times (in most implementations).

                  現(xiàn)在,要選擇方法,我們必須比較這兩個值: n * T ?n - 1.
                  因此,要選擇合適的方法,請按上述方法計算 T.如果 T <(n - 1)/n 最好使用第一種方法.否則使用第二種方法.

                  Now, to choose the method, we have to compare these two values: n * T ? n - 1.
                  So, to choose the appropriate method, calculate T as described above. If T < (n - 1)/n it's better to use the first method. Use the second method otherwise.

                  這篇關(guān)于在 [0..n-1] 范圍內(nèi)生成 m 個不同的隨機數(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 類比是什么?)

                  1. <small id='7UcYv'></small><noframes id='7UcYv'>

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

                        <legend id='7UcYv'><style id='7UcYv'><dir id='7UcYv'><q id='7UcYv'></q></dir></style></legend>
                        • <bdo id='7UcYv'></bdo><ul id='7UcYv'></ul>

                            主站蜘蛛池模板: www99热| 看片黄全部免费 | 国产呦小j女精品视频 | 欧美激情视频一区二区三区 | 精品久久一区二区 | www.成人在线| 亚洲男人的天堂在线观看 | 青草福利视频 | 日本久久久久 | 四虎影视大全 | 天天拍夜夜操 | 日本成片网 | 亚洲综合精品 | 1024日韩| 国产精品欧美精品 | 日本一区二区高清 | 成人动漫一区二区 | 91性高潮久久久久久久久 | 欧美午夜精品久久久久免费视 | 日韩精品福利 | av久久 | 国产精品成人网 | 亚洲福利一区二区 | 欧美国产视频 | 日韩精品一区二区视频 | 天天看天天爽 | 欧美精品一二三 | 午夜视频免费看 | 免费网站观看www在线观看 | 国产这里只有精品 | 一区二区三区中文字幕 | 欧美色影院 | 日韩中文一区 | 久久国产精品一区二区 | 免费的黄色小视频 | 黄色草逼视频 | 狠狠干狠狠操 | 伊人成人在线 | 欧美性精品| 欧美一级做性受免费大片免费 | 欧美三级又粗又硬 |