生成固定长度的随机数组(Java)

时间:2019-11-15 10:21:56

标签: java arrays random

我只是想更改我的代码,以便每次运行该代码时都会生成一个固定长度为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]); 

      } 

  } 

   } 

4 个答案:

答案 0 :(得分:2)

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);
  } 

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

答案 1 :(得分:1)

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

  1. nextInt()一一填写
  2. ints​(long)获得固定大小的IntStream,可以使用.toArray()轻松地将其转换为数组

答案 2 :(得分:1)

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

    //each shift must have at least 2 CSC
    int csc = 4;
    int days = 1;
    int shifts = 4;
    int shiftCSC = 2;
    IntVar[][][] shifts = new IntVar[csc][days][shifts];


    for (int n = 0; n < csc; n++) {
        for (int d = 0; d < days; d++) {
            for (int s = 0; s < shifts; s++) {
              shifts[n][d][s] = model.newIntVar(0, 1, "shift_n" + n + "d" + s + "s" + s);                   
            }
        }
    }

    for (int n = 0; n < csc; n++) {
        for (int d = 0; d < days; d++) {
            model.addEquality(LinearExpr.sum(shifts[n][d]), shiftCSC);

        }
    }

    for (int d = 0; d < days; d++) {
        for (int s = 0; s < shifts; s++) {
           IntVar[] nursesPerShift = new IntVar[csc];    
           for (int n = 0; n < csc; n++) {
                nursesPerShift[n] = shifts[n][d][s];
           }
           model.addGreaterOrEqual(LinearExpr.sum(nursesPerShift), 2);
        }
   }

   //OUTPUT
     CSC 1 works shift 0
     CSC 1 works shift 2
     CSC 2 works shift 1
     CSC 2 works shift 3
     CSC 3 works shift 1
     CSC 3 works shift 2
     CSC 4 works shift 0
     CSC 4 works shift 3

   //expected output
     CSC 1 works shift 0
     CSC 1 works shift 1
     CSC 2 works shift 2
     CSC 2 works shift 3
     CSC 3 works shift 0
     CSC 3 works shift 1
     CSC 4 works shift 2
     CSC 4 works shift 3

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

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

即使我认为摆脱数组并仅使用List也是更明智的选择。

答案 3 :(得分:0)

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

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]