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

<small id='5Lmnx'></small><noframes id='5Lmnx'>

      <tfoot id='5Lmnx'></tfoot>

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

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

        無重復(fù)的隨機(jī)數(shù)組生成

        Random array generation with no duplicates(無重復(fù)的隨機(jī)數(shù)組生成)

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

              <i id='qIr1J'><tr id='qIr1J'><dt id='qIr1J'><q id='qIr1J'><span id='qIr1J'><b id='qIr1J'><form id='qIr1J'><ins id='qIr1J'></ins><ul id='qIr1J'></ul><sub id='qIr1J'></sub></form><legend id='qIr1J'></legend><bdo id='qIr1J'><pre id='qIr1J'><center id='qIr1J'></center></pre></bdo></b><th id='qIr1J'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='qIr1J'><tfoot id='qIr1J'></tfoot><dl id='qIr1J'><fieldset id='qIr1J'></fieldset></dl></div>
                <legend id='qIr1J'><style id='qIr1J'><dir id='qIr1J'><q id='qIr1J'></q></dir></style></legend>
                  <bdo id='qIr1J'></bdo><ul id='qIr1J'></ul>
                    <tbody id='qIr1J'></tbody>
                  <tfoot id='qIr1J'></tfoot>
                • 本文介紹了無重復(fù)的隨機(jī)數(shù)組生成的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

                  問題描述

                  我正在嘗試創(chuàng)建一些可以生成沒有重復(fù)值的隨機(jī)數(shù)組的東西.我已經(jīng)看過其他答案,但似乎沒有一個(gè)能幫助我理解.我想不出一種方法來實(shí)際生成不包含重復(fù)項(xiàng)的隨機(jī)數(shù).這是我迄今為止嘗試過的:

                  srand(time(NULL));整數(shù) [4];for (int x=0; x!=4;x++){數(shù)字[x] = 1 + (rand() % 4) ;printf("%d", 數(shù)字[x]);}

                  <塊引用>

                  任何幫助將不勝感激.

                  解決方案

                  首先rand() 是生成隨機(jī)數(shù),但不能重復(fù).

                  如果您想生成一個(gè)沒有重復(fù)項(xiàng)的隨機(jī)數(shù)組,rand() 方法根本不起作用.

                  假設(shè)您要生成一個(gè)1000個(gè)數(shù)字的數(shù)組.在最好的情況下,假設(shè)您生成了沒有重復(fù)的前 999 個(gè)數(shù)字,最后想做的是生成最后一個(gè)數(shù)字.獲得這個(gè)數(shù)字的概率是 1/1000,所以這幾乎需要很長時(shí)間才能生成.實(shí)際上,只有 10 個(gè)數(shù)字會(huì)帶來很大的麻煩.

                  最好的方法是通過增量(或嚴(yán)格單調(diào)序列)生成所有數(shù)字,即混洗它們.在這種情況下,將沒有重復(fù)

                  這里 是一個(gè)關(guān)于如何使用 10 個(gè)數(shù)字的示例.即使有 1000 個(gè)號(hào)碼,它也工作.

                  注意:來自 Jhon Leehey 的 答案.

                  #include #include #include void shuffle(int *arr, size_t n){如果 (n > 1){size_t i;srand(時(shí)間(空));for (i = 0; i 

                  I am trying to create something that generates a random array with no duplicate values. I've already looked at other answers but none seem to help me understand. I cannot think of a way to actually generate random numbers that contain no duplicates. Here is what i have tried so far:

                  srand(time(NULL));
                  int numbers [4];
                  
                  for (int x=0; x!=4;x++)
                  {
                      numbers[x] = 1 + (rand() % 4) ;
                      printf("%d ", numbers[x]);
                  }
                  

                  Any help will be appreciated.

                  解決方案

                  First of all rand() is generatig random numbers but not wihout duplicates.

                  If you want to generate a random array without duplicates the rand() method is not working at all.

                  Let say you want to generate an array of 1000 numbers. In the best case let say you generated the first 999 numbers without duplicates and last think to do is generating the last number. The probability of getting that number is 1/1000 so this is almost going to take forever to get generated. In practice only 10 numbers makes a big trouble.

                  The best method is to generate all your numbers by incrementation (or strictly monotonic sequence) is shuffle them. In this case there will be no duplicates

                  Here is an exemple on how to do it with 10 numbers. Even with 1000 numbers it's working.

                  Note: Suffle function from Jhon Leehey's answer.

                  #include <stdio.h>
                  #include <stdlib.h>
                  #include <time.h>
                  
                  void shuffle(int *arr, size_t n)
                  {
                      if (n > 1) 
                      {
                          size_t i;
                          srand(time(NULL));
                          for (i = 0; i < n - 1; i++) 
                          {
                            size_t j = i + rand() / (RAND_MAX / (n - i) + 1);
                            int t = arr[j];
                            arr[j] = arr[i];
                            arr[i] = t;
                          }
                      }
                  }
                  
                  int main()
                  {
                      int i;
                      int arr[10];
                      for (i=0; i<10; i++){
                          arr[i] = i;
                      }
                      shuffle(arr, 10);
                      for (i=0; i<10; i++){
                          printf("%d ", arr[i]);
                      }
                  }
                  

                  這篇關(guān)于無重復(fù)的隨機(jī)數(shù)組生成的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

                  【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(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() 之后沒有 getline(cin, var) 讀取字符串的第一個(gè)字符?)
                  What is the cin analougus of scanf formatted input?(scanf 格式輸入的 cin 類比是什么?)
                      <bdo id='6jp20'></bdo><ul id='6jp20'></ul>
                    • <small id='6jp20'></small><noframes id='6jp20'>

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

                          <tbody id='6jp20'></tbody>
                      2. <i id='6jp20'><tr id='6jp20'><dt id='6jp20'><q id='6jp20'><span id='6jp20'><b id='6jp20'><form id='6jp20'><ins id='6jp20'></ins><ul id='6jp20'></ul><sub id='6jp20'></sub></form><legend id='6jp20'></legend><bdo id='6jp20'><pre id='6jp20'><center id='6jp20'></center></pre></bdo></b><th id='6jp20'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='6jp20'><tfoot id='6jp20'></tfoot><dl id='6jp20'><fieldset id='6jp20'></fieldset></dl></div>
                          <tfoot id='6jp20'></tfoot>
                            主站蜘蛛池模板: 风间由美一区二区三区在线观看 | 国产日韩欧美一区 | 污视频免费在线观看 | 欧美视频第三页 | 免费色网址 | 欧美日韩中文在线观看 | 日本成人午夜影院 | 国产视频中文字幕 | 男人天堂网址 | 国产精品久久久久久网站 | 国产在线精品一区二区三区 | 欧美一区二区三区在线 | 性天堂网 | 成人国产精品免费观看 | 亚洲激情av| 天堂久久久久久久 | 特级a欧美做爰片毛片 | 91免费看片 | 免费一级欧美在线观看视频 | 国产精品伦一区二区三级视频 | 欧美黄色片在线观看 | 日本啊v在线| 在线视频中文字幕 | 91网站在线观看视频 | 日韩成人av在线 | 天堂在线91 | 精品99久久 | 中文字幕加勒比 | 视频一区中文字幕 | 亚洲一区二区不卡在线观看 | 亚洲视频 欧美视频 | 欧美激情在线一区二区三区 | 东方伊人免费在线观看 | 女同久久另类99精品国产 | 日韩精品二区 | 一级黄色片网址 | 中文在线一区二区 | 亚洲综合色自拍一区 | 中文字幕中文字幕 | 日本一二区视频 | 亚洲精品电影网在线观看 |