【问题标题】:Generating random array of fixed length [duplicate]生成固定长度的随机数组
【发布时间】:2020-03-11 11:36:32
【问题描述】:

我只是想更改我的代码,以便在每次运行代码时生成一个固定长度为 100 个整数的随机数组,而不仅仅是在代码中包含一个预设数组。我对此很陌生,所以只需要一个正确方向的指南,谢谢

public class Selectionsort {
    public static void main(String[] args) {
        int[] numbers = {15, 8, 6, 21, 3, 54, 6, 876, 56, 12, 1, 4, 9};
        sort(numbers);
        printArray(numbers);
    }

    public static int[] sort(int[] A) {
        for (int i = 0; i < A.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < A.length; j++) {
                if (A[j] < A[minIndex]) {
                    minIndex = j;
                }
            }
            int temp = A[minIndex];
            A[minIndex] = A[i];
            A[i] = temp;
        }
        return A;
    }

    public static void printArray(int[] A) {
        for (int i = 0; i < A.length; i++) {
            System.out.println(A[i]);
        }
    }
}

【问题讨论】:

  • 显然你已经知道循环了。所以你需要弄清楚的是如何在Java中生成随机整数。当您在 Google 上搜索“如何在 Java 中生成随机整数”时会发生什么?

标签: java arrays random


【解决方案1】:

下面将用 1-1000 的随机数填充 100 个整数的数组

int[] numbers = new int[100];
Random rand = new Random();
for (int i = 0; i < numbers.length; i++) {
    numbers[i] = rand.nextInt(1000);
}

但请注意,上述代码可能会插入重复项。如果你想避免这种情况,使用与数组并行的 List 并检查生成的值是否已经存在应该确保唯一性:

int[] numbers = new int[100];
List<Integer> numbersList = new ArrayList<Integer>(numbers.length);
Random rand = new Random();
for (int i = 0; i < numbers.length; i++) {
    int j = rand.nextInt(1000);
    while (numbersList.contains(j)) {
        j = rand.nextInt(1000);
    }
    numbers[i] = j;
    numbersList.add(j);
}

尽管我认为摆脱数组并仅使用 List 会更明智...

【讨论】:

    【解决方案2】:

    您可以使用Math.random 方法。此代码在[50,60] 范围内生成一个10 随机数数组。

    int length = 10, min = 50, max = 60;
    int[] arr = IntStream.range(0, length)
            .map(i -> (int) (min + Math.random() * (max - min + 1)))
            .toArray();
    
    System.out.println(Arrays.toString(arr));
    // [54, 51, 58, 59, 57, 56, 56, 54, 54, 58]
    

    全套11随机数:

    Set<Integer> set = new HashSet<>();
    while (set.size() < max - min + 1) {
        set.add((int) (min + Math.random() * (max - min + 1)));
    }
    
    System.out.println(set);
    // [50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60]
    

    【讨论】:

      【解决方案3】:
      public class Selectionsort {
          public static void main(String[] args) {
              int[] numbers = new int[100]
              Random random = new Random();
      
              for (int i = 0; i < numbers.length; i++) {
                  numbers[i] = random.nextInt(100);
              }
              sort(numbers);
      
              printArray(numbers);
          }
      }
      

      上面的 sn-p 将帮助您为大小为 100 的数组创建一个介于 1 到 100 之间的随机数。

      【讨论】:

        【解决方案4】:

        您应该查看Random 类。 更具体地说:

        1. nextInt()一一填写
        2. ints​(long) 得到一个固定大小的 IntStream ,可以很容易地用.toArray() 转换成数组

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-06-19
          • 2012-01-18
          • 2016-06-17
          • 2015-09-13
          • 1970-01-01
          • 2021-03-15
          • 2014-03-16
          • 2019-06-23
          相关资源
          最近更新 更多