用 java 语言获取 1--N 的不重复随机数

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class H {
	public static void main(String args[]) {
		int N = 10;
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < N; i++) {
			list.add(i + 1);
		}
		int count = N;
		int items[] = new int[N];
		for (int i = 0; i < N; i++) {
			// 
			int randomInt = new Random().nextInt(count);
			items[i] = list.get(randomInt);
			list.remove(randomInt);
			count--;
			System.out.println(items[i]);
		}
	}
}


你可能感兴趣的:(java,Random)