久久久久久久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] 范圍內生成 m 個不同的隨機數

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

        <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] 范圍內生成 m 個不同的隨機數的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

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

                  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;
                  

                  當 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? :)

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

                  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)

                  推薦答案

                  純數學:
                  讓我們計算兩種情況下 rand() 函數調用的數量并比較結果:

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

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

                  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 調用獲得它的概率是p.使用 2 調用 - q * p,其中 q = 1 - p.在一般情況下,在 n 次調用之后準確獲得它的概率是 (q^(n-1))*p.因此,數學期望為
                  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 上,您將執行 1/p = n/(nk) 調用 rand()功能.

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

                  現在讓我們總結一下:

                  Sum[ n/(n - k) ], k = 0 -->m - 1 = n * T - 方法 1 中的 rand 調用次數.
                  這里 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 - 1 次(在大多數實現中).

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

                  現在,要選擇方法,我們必須比較這兩個值: 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.

                  這篇關于在 [0..n-1] 范圍內生成 m 個不同的隨機數的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 類比是什么?)

                  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>

                            主站蜘蛛池模板: 国产成人99久久亚洲综合精品 | 亚洲精品在线观看视频 | 久久综合久久久 | 亚洲精品久久久久久国产精华液 | 国产一区二区欧美 | 91精品一区二区三区久久久久久 | 欧美日韩久久 | 欧美日韩国产精品激情在线播放 | 玖玖综合网 | 中文字幕一区在线观看视频 | 久久国内 | 一区二区三区在线播放视频 | 亚洲一区二区三区四区视频 | 在线国产小视频 | 精品毛片在线观看 | 日韩一二三区视频 | 91佛爷在线观看 | 国产色99| 久久成人18免费网站 | 欧洲在线视频 | 中文字幕av在线播放 | 福利视频网站 | 亚洲精品欧美 | 成人精品一区二区三区中文字幕 | 欧美aaa一级片 | 一本综合久久 | 国产露脸国语对白在线 | 国产特黄一级 | 国产精品美女久久久免费 | 日本久久一区二区三区 | 欧美一级免费片 | 亚洲一区二区三区桃乃木香奈 | 91精品一区二区三区久久久久 | 国产精品久久久久久久久久软件 | 欧美1区2区 | 黄免费在线 | 三级在线视频 | 国产福利资源在线 | 天天干天天插天天 | 国产做爰 | 日本激情视频网 |