<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 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>
        <tfoot id='qIr1J'></tfoot>
      1. 无重复的随机数组生成

        时间:2023-10-07

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

          <tbody id='5Lmnx'></tbody>
          <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 id='5Lmnx'><tfoot id='5Lmnx'></tfoot><dl id='5Lmnx'><fieldset id='5Lmnx'></fieldset></dl></div>
                  <bdo id='5Lmnx'></bdo><ul id='5Lmnx'></ul>
                  本文介绍了无重复的随机数组生成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在尝试创建一些可以生成没有重复值的随机数组的东西.我已经看过其他答案,但似乎没有一个能帮助我理解.我想不出一种方法来实际生成不包含重复项的随机数.这是我迄今为止尝试过的:

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

                  <块引用>

                  任何帮助将不胜感激.

                  解决方案

                  首先rand() 是生成随机数,但不能重复.

                  如果您想生成一个没有重复项的随机数组,rand() 方法根本不起作用.

                  假设您要生成一个1000个数字的数组.在最好的情况下,假设您生成了没有重复的前 999 个数字,最后想做的是生成最后一个数字.获得这个数字的概率是 1/1000,所以这几乎需要很长时间才能生成.实际上,只有 10 个数字会带来很大的麻烦.

                  最好的方法是通过增量(或严格单调序列)生成所有数字,即混洗它们.在这种情况下,将没有重复

                  这里 是一个关于如何使用 10 个数字的示例.即使有 1000 个号码,它也工作.

                  注意:来自 Jhon Leehey 的 答案.

                  #include #include #include void shuffle(int *arr, size_t n){如果 (n > 1){size_t i;srand(时间(空));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]);
                      }
                  }
                  

                  这篇关于无重复的随机数组生成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                      <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 id='6jp20'><tfoot id='6jp20'></tfoot><dl id='6jp20'><fieldset id='6jp20'></fieldset></dl></div>
                          <tfoot id='6jp20'></tfoot>