Java 随机化方法的问题
我有一个方法不能正常工作。 该方法假定对1到20之间的一组数字进行随机排序(每个数字 必须只出现一次)。 我的问题是,当我运行程序时,一些数字会重复几次。 代码如下:Java 随机化方法的问题,java,algorithm,sorting,methods,random,Java,Algorithm,Sorting,Methods,Random,我有一个方法不能正常工作。 该方法假定对1到20之间的一组数字进行随机排序(每个数字 必须只出现一次)。 我的问题是,当我运行程序时,一些数字会重复几次。 代码如下: public static int randomize(int index) { //This array will hold the 20 numbers. int[] randomIndex = new int[20]; Random ranNum = new Random(); for
public static int randomize(int index) {
//This array will hold the 20 numbers.
int[] randomIndex = new int[20];
Random ranNum = new Random();
for (int x = 0; x<20; x++) {
int temp;
//The number is generated randomly and saved in temp.
temp = ranNum.nextInt(20);
//This loop skips the first index.
if (x != 0){
/*Here, the loop is supposed to compare a generated number with
the previous one*/
for (int y = 1; y<=x; y++) {
while(temp == randomIndex[x-y] ) {
/*If the while loop finds that temp variable matches any previous
number it will generate another random number for it until it finds
no matches.*/
temp = ranNum.nextInt(20);
}
}
}
/*Once no match has been found for temp, the number is assigned to an index,
and the loop is executed with a x variable increment.
randomIndex[x] = temp;
}
//Finally the array with the set of random numbers is sent to the main function.
return randomIndex[index];
}
所以现在我不知道该怎么办C使用
Random.nextInt()
时,无法保证生成的数字是唯一的。
您应该首先生成从1到20的数字,然后将这些数字洗牌。现在问题变成了“如何随机洗牌?”
也许您可以参考JDKCollections.shuffle()
的实现
洗牌数字的算法很简单:
您可以使用以下方法来避免:
final Random random = new Random();
final HashSet<Integer> integers = new HashSet<>();
while(integers.size() < 20) {
integers.add(random.nextInt(20));
}
System.out.println(integers);
for(int i=0; i<20; i++) { // index goes from 0..19
randomIndex[i] = i + 1; // value goes from 1..20
}
for(int i=0; i<20; i++) {
int j = i + ranNum.nextInt(20 - i); // choose random j from i <= j < 20
int temp = randomIndex[i]; // swap elements i and j
randomIndex[i] = randimIndex[j];
randomIndex[j] = temp;
}
final Random Random=new Random();
最终HashSet整数=新HashSet();
while(integers.size()<20){
整数.add(random.nextInt(20));
}
System.out.println(整数);
看起来您正试图通过拒绝生成随机数——也就是说,将每个随机数与以前接受的所有数字进行比较,然后重新生成新的随机数,直到找到一个与所有随机数不同的随机数
正如其他人所提到的,生成从1到20的数字,并用随机排列将其洗牌,效率会高得多。但是,如果正确实施,您的方法应该会起作用。。。最终
随机洗牌实现可能如下所示:
final Random random = new Random();
final HashSet<Integer> integers = new HashSet<>();
while(integers.size() < 20) {
integers.add(random.nextInt(20));
}
System.out.println(integers);
for(int i=0; i<20; i++) { // index goes from 0..19
randomIndex[i] = i + 1; // value goes from 1..20
}
for(int i=0; i<20; i++) {
int j = i + ranNum.nextInt(20 - i); // choose random j from i <= j < 20
int temp = randomIndex[i]; // swap elements i and j
randomIndex[i] = randimIndex[j];
randomIndex[j] = temp;
}
for(int i=0;i我编辑了生成数组的函数,从1到20:
public static int[] randomize() {
int[] randomIndex = new int[20];
Random ranNum = new Random();
boolean isAlreadyIn;
boolean isZero;
int x = 0;
while (x < 20) {
isAlreadyIn = false;
isZero = false;
int temp;
temp = ranNum.nextInt(21);
for(int i = 0; i < randomIndex.length; i++){
if(temp == 0)
isZero = true;
if(temp == randomIndex[i])
isAlreadyIn = true;
}
if (!isZero && !isAlreadyIn){
randomIndex[x] = temp;
x++;
}
}
return randomIndex;
}
公共静态int[]随机化(){
int[]随机索引=新的int[20];
Random ranNum=新的Random();
布尔isAlreadyIn;
布尔值为零;
int x=0;
而(x<20){
isAlreadyIn=错误;
isZero=假;
内部温度;
温度=下一个数量(21);
对于(int i=0;i
希望这会有帮助。你能添加一些关于你的代码应该做什么的注释吗?我想这也会帮助你调试!为什么不使用列表和Collections.shuffle()
方法?或者至少将你已经绘制的每个数字添加到列表中,并检查它是否与List.contains(newNumber)重复?重复:您的主要问题是,您每次只能确保您使用的号码在列表中一个号码,这意味着您实际上没有检查以确保它在整个列表中。。