How to select random values from a given range

前端 未结 6 638
一整个雨季
一整个雨季 2021-01-06 10:44

I\'m trying to create an android application which will generate random series of values (integer numbers in this case) in a given range (but NOT equal betw

6条回答
  •  Happy的楠姐
    2021-01-06 11:18

    int low = ...;
    int high = ...;
    List choices = new LinkedList();
    for (int i = low; i <= high; i++) {
        choices.add(i);
    } 
    
    Collections.shuffle(choices);
    
    int[] choices = new int[] {
      choices.get(0),
      choices.get(1),
      choices.get(2),
      choices.get(3),
      choices.get(4)
    };
    

提交回复
热议问题