# Java生成非重复的随机数

``import java.util.Random; public class Sort{ public static void main(String[] args){ int[] nums = new int[10000]; Random randomGenerator = new Random(); for (int i = 0; i < nums.length; ++i){ nums[i] = randomGenerator.nextInt(10000); } } }` `

` `Integer[] arr = {...}; Collections.shuffle(Arrays.asList(arr));` `

` `public static void main(String[] args) { Integer[] arr = new Integer[1000]; for (int i = 0; i < arr.length; i++) { arr[i] = i; } Collections.shuffle(Arrays.asList(arr)); System.out.println(Arrays.toString(arr)); }` `

` `public static int[] sampleRandomNumbersWithoutRepetition(int start, int end, int count) { Random rng = new Random(); int[] result = new int[count]; int cur = 0; int remaining = end - start; for (int i = start; i < end && count > 0; i++) { double probability = rng.nextDouble(); if (probability < ((double) count) / (double) remaining) { count--; result[cur++] = i; } remaining--; } return result; }` `

Achintya Jha在这里有正确的想法。 不要考虑如何删除重复项，而是首先删除创build重复项的function。

1. 创build大小为n的数组。
2. 循环并将索引i中的每个值初始化为值i（或如果希望将数字1到n而不是0到n-1，则为i + 1）。
3. 最后，循环遍历数组，再次交换每个值的随机索引值。

` `import java.util.Random; public class Sort { // use a constant rather than having the "magic number" 10000 scattered about public static final int N = 10000; public static void main(String[] args) { //array to store N random integers (0 - N-1) int[] nums = new int[N]; // initialize each value at index i to the value i for (int i = 0; i < nums.length; ++i) { nums[i] = i; } Random randomGenerator = new Random(); int randomIndex; // the randomly selected index each time through the loop int randomValue; // the value at nums[randomIndex] each time through the loop // randomize order of values for(int i = 0; i < nums.length; ++i) { // select a random index randomIndex = randomGenerator.nextInt(nums.length); // swap values randomValue = nums[randomIndex]; nums[randomIndex] = nums[i]; nums[i] = randomValue; } } }` `

` `Integer[] arr = new Integer[((int) (Math.random() * (16 - 30) + 30))]; for (int i = 0; i < arr.length; i++) { arr[i] = i; } Collections.shuffle(Arrays.asList(arr)); System.out.println(Arrays.toString(arr));`` `

`[1, 10, 2, 4, 9, 8, 7, 13, 18, 17, 5, 21, 12, 16, 23, 20, 6, 0, 22, 14, 24, 15, 3, 11, 19]`

` `LinkedHashSet<Integer> test = new LinkedHashSet<Integer>(); Random random = new Random(); do{ test.add(random.nextInt(1000) + 1); }while(test.size() != 1000);` `

` `public class Randoms { static int z, a = 1111, b = 9999, r; public static void main(String ... args[]) { rand(); } public static void rand() { Random ran = new Random(); for (int i = 1; i == 1; i++) { z = ran.nextInt(b - a + 1) + a; System.out.println(z); randcheck(); } } private static void randcheck() { for (int i = 3; i >= 0; i--) { if (z != 0) { r = z % 10; arr[i] = r; z = z / 10; } } for (int i = 0; i <= 3; i++) { for (int j = i + 1; j <= 3; j++) { if (arr[i] == arr[j]) { rand(); } } } } }` `