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

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

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

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

      <tfoot id='VfiWE'></tfoot>

        模數和 rand() 如何工作?

        How does modulus and rand() work?(模數和 rand() 如何工作?)
          <bdo id='W73Lh'></bdo><ul id='W73Lh'></ul>

                <tbody id='W73Lh'></tbody>

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

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

                1. <legend id='W73Lh'><style id='W73Lh'><dir id='W73Lh'><q id='W73Lh'></q></dir></style></legend>
                2. 本文介紹了模數和 rand() 如何工作?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  所以,我一直對這個很著迷.

                  So, I've been nuts on this.

                  rand() % 6 將始終產生 0-5 之間的結果.

                  rand() % 6 will always produce a result between 0-5.

                  但是,當我需要介于兩者之間時,可以說是 6-12.

                  However when I need between, let's say 6-12.

                  我應該有 rand() % 6 + 6

                  Should I have rand() % 6 + 6

                  0+6 = 6.
                  1+6 = 7.
                  ...
                  5+6 = 11. ???
                  

                  如果我想要間隔 6-12,那么我需要 +7 嗎?但是,0+7 = 7.什么時候會隨機6個?

                  So do I need to + 7 If I want the interval 6-12? But then, 0+7 =7. When will it randomize 6?

                  我在這里錯過了什么?哪個是在 6 到 12 之間隨機數的正確方法?為什么?我好像在這里遺漏了什么.

                  What am I missing here? Which one is the correct way to have a randomized number between 6 and 12? And why? It seems like I am missing something here.

                  推薦答案

                  如果 C++11 是一個選項,那么您應該使用 隨機標頭 和 uniform_int_distrubution.正如 James 在評論中指出的那樣,使用 rand% 有很多問題,包括偏差分布:

                  If C++11 is an option then you should use the random header and uniform_int_distrubution. As James pointed out in the comments using rand and % has a lot of issues including a biased distribution:

                  #include <iostream>
                  #include <random>
                  
                  int main()
                  {
                      std::random_device rd;
                  
                      std::mt19937 e2(rd());
                  
                      std::uniform_int_distribution<int> dist(6, 12);
                  
                      for (int n = 0; n < 10; ++n) {
                              std::cout << dist(e2) << ", " ;
                      }
                      std::cout << std::endl ;
                  }
                  

                  如果您必須使用 rand 那么應該這樣做:

                  if you have to use rand then this should do:

                  rand() % 7 + 6
                  

                  更新

                  使用 rand 的更好方法如下:

                  A better method using rand would be as follows:

                  6 + rand() / (RAND_MAX / (12 - 6 + 1) + 1)
                  

                  我從 C 常見問題解答 中獲得了這個,并解釋了 如何我得到一定范圍內的隨機整數? 問題.

                  I obtained this from the C FAQ and it is explained How can I get random integers in a certain range? question.

                  更新 2

                  Boost 也是一種選擇:

                  #include <iostream>
                  #include <boost/random/mersenne_twister.hpp>
                  #include <boost/random/uniform_int_distribution.hpp>
                  
                  int main()
                  {
                    boost::random::mt19937 gen;
                    boost::random::uniform_int_distribution<> dist(6, 12);
                  
                    for (int n = 0; n < 10; ++n) {
                      std::cout << dist(gen) << ", ";
                    }
                    std::cout << std::endl ;
                  }
                  

                  這篇關于模數和 rand() 如何工作?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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 類比是什么?)

                    <tbody id='crp1o'></tbody>

                  1. <tfoot id='crp1o'></tfoot>

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

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

                            <bdo id='crp1o'></bdo><ul id='crp1o'></ul>
                          • <legend id='crp1o'><style id='crp1o'><dir id='crp1o'><q id='crp1o'></q></dir></style></legend>
                          • 主站蜘蛛池模板: 99久久亚洲 | 日韩成人精品 | 亚洲精品在线免费观看视频 | 国产免费一区二区 | 精品欧美色视频网站在线观看 | 欧美视频成人 | 国产一级视频在线播放 | 精品96久久久久久中文字幕无 | 一区二区三区四区免费视频 | 欧美精品乱码久久久久久按摩 | 蜜桃精品在线 | 欧美精品久久久 | 91九色视频 | 久久久久综合 | 在线播放中文字幕 | 国外成人在线视频网站 | 日韩av在线一区二区三区 | 国产精品综合色区在线观看 | 99草免费视频 | 欧美日韩在线一区二区 | 亚洲欧美成人 | 四虎永久在线精品免费一区二 | 欧美一级片免费看 | wwwxxx国产 | 你懂的免费在线 | 精品视频一区二区三区在线观看 | 天天爽夜夜爽精品视频婷婷 | 日韩欧美国产精品 | 午夜av电影院 | 天天曰天天曰 | 欧美日韩视频在线第一区 | 99精品在线免费观看 | 99久久久久 | 亚洲狠狠爱一区二区三区 | 国产一区不卡 | 国产精品日韩欧美一区二区三区 | 国产一级特黄aaa大片评分 | 人人九九精 | 九九热在线免费观看 | 成人特级毛片 | 日本精品视频 |