Java洗牌实现

import java.util.*;

public class LatteryDrawing{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        System.out.println("The number of you draw:");

        int number = scanner.nextInt();

        int[] numbers = new int[number];
        for(int i = 0;i < number;i ++){
            numbers[i] = i + 1;
        }
        System.out.println(Arrays.toString(numbers));
        
        int arrayLength = numbers.length;
        int randomIndex = numbers.length;
        
        int[] results = new int[arrayLength];
        
        while(randomIndex > 0){
            int r = (int)(Math.random() * randomIndex);
            results[arrayLength - randomIndex] = numbers[r];
            numbers[r] = numbers[randomIndex - 1];
            randomIndex --;
        }

        System.out.println(Arrays.toString(results));

        Arrays.sort(results);
        System.out.println(Arrays.toString(results));
    }
}

你可能感兴趣的:(Java洗牌实现)