Pick a few random numbers in a specific range with

2019-09-09 17:24发布

I want to pick 8 random integers in the range 0-7.

int one = (int) (Math.random()*0)+7;
int two = (int) (Math.random()*0)+7;
// ...
int eight = (int) (Math.random()*0)+7;

However, no duplicate numbers are allowed. How can I improve this code to establish this?

标签: java random
5条回答
倾城 Initia
2楼-- · 2019-09-09 17:49

I'm not sure if this violates your requirements, but you could just go like this:

public static void main(String[] args) {
    List<Integer> randomZeroToSeven = new ArrayList<>();
    for (int i = 0; i <= 7; i++) {
        randomZeroToSeven.add(i);
    }
    Collections.shuffle(randomZeroToSeven);
}

Alternatively, you can skip the shuffle and just grab a random element out of the list every time you want a random number from 0-7. EG:

public static void main(String[] args) {
    List<Integer> zeroToSeven = new ArrayList<>();
    for (int i = 0; i <= 7; i++) {
        zeroToSeven.add(i);
    }

    System.out.println(zeroToSeven.get(new Random().nextInt(8)));

}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-09-09 17:54

Follow the below snippet and change the maxLimit and noOfItems according to your preference to get desired result.

Here the Set contains unique integers under specified limit.

public class RandomIntegers
{
    public static final Random random = new Random();
    public static final int maxLimit = 8;
    public static final int noOfItems = 8;
    public static void main( String[] args )
    {
        Set<Integer> uniqueRandomIntegerSet = new HashSet< Integer >(); 
        
        while(uniqueRandomIntegerSet.size() < noOfItems)
            uniqueRandomIntegerSet.add( random.nextInt( maxLimit ) );
    }
}

查看更多
乱世女痞
4楼-- · 2019-09-09 17:55

Another way is to fill up an array / collection with all possibilities and pick from those. Each picked item is removed from the allowed array and put into the output array until you have picked all items.

This way you skip double picks and the execution is linear.

Something like this:

int desiredSize = 8;
List<Integer> allowed = new ArrayList<>();
for (int i = 0; i < desiredSize; i++) {
    allowed.add(i);
}

List<Integer> output = new ArrayList<>();
Random random = new Random();
while (output.size() < desiredSize) {
    int index = random.nextInt(allowed.size());
    output.add(allowed.get(index));
    allowed.remove(index);
}
查看更多
别忘想泡老子
5楼-- · 2019-09-09 18:00

If you need to generate numbers from min to max (including both), you can write random.nextInt(max - min + 1) + min

public static void main(String[] args) throws IOException {

        HashSet<Integer>list=new HashSet();
       Random random=new Random();
         while(list.size()<8){
            list.add(random.nextInt(7 - 0 + 1) + 0); // OR list.add(random.nextInt(8));

         }
         System.out.println(list);

    }
查看更多
何必那么认真
6楼-- · 2019-09-09 18:00

There are only 8 integers from 0-7 and so what you are really asking is for the numbers from 0-7 in a random order.

The quickest way to do this both coding and execution is to shuffle a set of integers.

public class Test {

    public static void main(String[] args){

        List<Integer> list = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7);
        Collections.shuffle(list);
        for(int i : list){
            System.out.println(i);
        }

    }
}

If you want to write a shuffle algorithm yourself you can do so by so swapping every item (bar the last) in an array with a random index. The reason you don't do the last is because it biases the outcome. See Fisher Yates Shuffle

Just because I couldn't resist here is a java implementation of Fisher Yates:

public class Test {

    public static void main(String[] args){
        Random rnd = new SecureRandom();
        int[] arr = new int[]{0,1,2,3,4,5,6,7};
        for(int i = 0; i < arr.length - 1; i++){
            int swapIndex = rnd.nextInt(8);
            int tmp = arr[i];
            arr[i] = arr[swapIndex];
            arr[swapIndex] = tmp;
        }
        System.out.println(Arrays.toString(arr));

    }

}
查看更多
登录 后发表回答