生成的Array随机数 [英] Generate Random Numbers in Array
问题描述
可能重复:
独特的随机数为O(1)?
我是新Java编写的。我想从给定的集一集的随机数,这个数字也必须不能重复。例如,可能的数字是 [0,1,2,3]
,我想获得存储在数组中三个随机唯一的编号。例 [0,2,1],[2,3,1],[0,3,2]
等。
I am new in Java. I want to generate a set of random numbers from a given set and the numbers must also not repeat. For example, the possible numbers are [0,1,2,3]
, I want to get three random unique numbers stored in an Array. Ex. [0,2,1], [2,3,1], [0,3,2]
etc.
推荐答案
您需要一个费雪耶茨洗牌。
You need a Fisher-Yates shuffle.
这是一个非常有效的选择不适用,从M的解决方案,让您的值的子集的零的副本的可能性(和任何不必要的前期排序)。伪code要做到这一点如下:
This is a very efficient "select n from m" solution that gives you a subset of your values with zero possibility of duplicates (and no unnecessary up-front sorting). Pseudo-code to do this follows:
dim n[N] // gives n[0] through n[N-1]
for each i in 0..N-1:
n[i] = i // initialise them to their indexes
nsize = N // starting pool size
do N times:
i = rnd(nsize) // give a number between 0 and nsize-1
print n[i]
nsize = nsize - 1 // these two lines effectively remove the used number
n[i] = n[nsize]
通过简单地从池中(基于当前池的大小)选择一个随机数,从池上面的数字替换它,然后还原池的大小,你会得到一个洗牌,而不必担心大掉了前面的号码。
By simply selecting a random number from the pool (based on the current pool size), replacing it with the top number from that pool, then reducing the size of the pool, you get a shuffle without having to worry about a large number of swaps up front.
这是很重要的,如果数字高,因为它不引入不必要的启动延迟。
This is important if the number is high in that it doesn't introduce an unnecessary startup delay.
例如,检查下面的板凳检查,选择10 - 从 - 10:
For example, examine the following bench-check, choosing 10-from-10:
<------ n[] ------>
0 1 2 3 4 5 6 7 8 9 nsize rnd(nsize) output
------------------- ----- ---------- ------
0 1 2 3 4 5 6 7 8 9 10 4 4
0 1 2 3 9 5 6 7 8 9 7 7
0 1 2 3 9 5 6 8 8 2 2
0 1 8 3 9 5 6 7 6 6
0 1 8 3 9 5 6 0 0
5 1 8 3 9 5 2 8
5 1 9 3 4 1 1
5 3 9 3 0 5
9 3 2 1 3
9 1 0 9
您可以看到池降低你去,因为你总是有一个未使用的替换用的,你永远不会有重复。
You can see the pool reducing as you go and, because you're always replacing the used one with an unused one, you'll never have a repeat.
这里有一个小Java程序,它显示了这个动作:
Here's a little Java program that shows this in action:
import java.util.Random;
public class testprog {
private int[] pool; // The pool of numbers.
private int size; // The current "size".
private Random rnd; // A random number generator.
// Constructor: just initilise the pool.
public testprog (int sz) {
pool = new int[sz];
size = sz;
rnd = new Random();
for (int i = 0; i < size; i++) pool[i] = i;
}
// Get next random number in pool (or -1 if exhausted).
public int next() {
if (size < 1) return -1;
int idx = rnd.nextInt(size--);
int rval = pool[idx];
pool[idx] = pool[size];
return rval;
}
// Test program for the pool.
public static void main(String[] args) {
testprog tp = new testprog (10);
for (int i = 0; i < 11; i++) System.out.println (tp.next());
}
}
的输出是(对于一个特定的运行):
The output is (for one particular run):
3
5
1
0
6
4
9
2
8
7
-1
在 1
在那里只是为了告诉你,当你用尽列表中会发生什么。既然你已经明确表示不想重复,它返回一个标记值。你也可以选择其他选项,如抛出一个异常,或只是重新启动池。
The -1
is there just to show you what happens when you exhaust the list. Since you've explicitly stated you don't want duplicates, it returns a sentinel value. You could also choose other options such as throwing an exception or just restarting the pool.
这篇关于生成的Array随机数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!