唯一随机生成的整数的Java数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20039025/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:33:47  来源:igfitidea点击:

Java Array of unique randomly generated integers

javaarrays

提问by AvP

public static int[] uniqueRandomElements (int size) {

    int[] a = new int[size];

    for (int i = 0; i < size; i++) {
        a[i] = (int)(Math.random()*10);

        for (int j = 0; j < i; j++) {
            if (a[i] == a[j]) {
                a[j] = (int)(Math.random()*10);
            }
        }   
    }

    for (int i = 0; i < a.length; i++) {
        System.out.print(a[i]+" ");
    }
    System.out.println();
    return a;
}

I have a method above which should generate an array of random elements that the user specifies. The randomly generated integers should be between 0 and 10 inclusive. I am able to generate random integers but the problem I have is checking for uniqueness. My attempt to check for uniqueness is in my code above but the array still contains duplicates of integers. What am I doing wrong and could someone give me a hint?

我上面有一个方法,它应该生成用户指定的随机元素数组。随机生成的整数应介于 0 和 10 之间。我能够生成随机整数,但我遇到的问题是检查唯一性。我在上面的代码中尝试检查唯一性,但数组仍然包含整数的重复项。我做错了什么,有人可以给我一个提示吗?

采纳答案by Justin

for (int i = 0; i < size; i++) {
    a[i] = (int)(Math.random()*10);

    for (int j = 0; j < i; j++) {
        if (a[i] == a[j]) {
            a[j] = (int)(Math.random()*10); //What's this! Another random number!
        }
    }   
}

You do find the duplicate values. However, you replace it with another random number that may be a duplicate. Instead, try this:

您确实找到了重复值。但是,您将其替换为另一个可能重复的随机数。相反,试试这个:

for (int i = 0; i < size; i++) {
    a[i] = (int)(Math.random()*10);//note, this generates numbers from [0,9]

    for (int j = 0; j < i; j++) {
        if (a[i] == a[j]) {
            i--; //if a[i] is a duplicate of a[j], then run the outer loop on i again
            break;
        }
    }  
}

However, this method is inefficient. I recommend making a list of numbers, then randomizing it:

然而,这种方法效率低下。我建议制作一个数字列表,然后将其随机化:

ArrayList<Integer> a = new ArrayList<>(11);
for (int i = 0; i <= 10; i++){ //to generate from 0-10 inclusive. 
                               //For 0-9 inclusive, remove the = on the <=
    a.add(i);
}
Collections.shuffle(a);
a = a.sublist(0,4);
//turn into array

Or you could do this:

或者你可以这样做:

ArrayList<Integer> list = new ArrayList<>(11);
for (int i = 0; i <= 10; i++){
    list.add(i);
}
int[] a = new int[size];
for (int count = 0; count < size; count++){
    a[count] = list.remove((int)(Math.random() * list.size()));
}

回答by Mihai Maruseac

If you have a duplicate you only regenerate the corresponding number once. But it might create another duplicate. You duplicate checking code should be enclosed in a loop:

如果您有重复,则只能重新生成相应的数字一次。但它可能会创建另一个副本。你重复的检查代码应该包含在一个循环中:

while (true) {
    boolean need_to_break = true;
    for (int j = 0; j < i; j++) {
        if (a[i] == a[j]) {
            need_to_break = false; // we might get another conflict
            a[j] = (int)(Math.random()*10);
        }
    }
    if (need_to_break) break;
}   

But make sure that sizeis less than 10, otherwise you will get an infinite loop.

但请确保size小于10,否则您将获得无限循环。

Edit: while the above method solves the problem, it is not efficient and should not be used for large sized arrays. Also, this doesn't have a guaranteed upper bound on the number of iterations needed to finish.

编辑:虽然上述方法解决了问题,但效率不高,不应用于大型数组。此外,这对完成所需的迭代次数没有保证的上限。

A better solution (which unfortunately only solves second point) might be to generate a sequence of the distinct numbers you want to generate (the 10numbers), randomly permute this sequence and then select only the first sizeelements of that sequence and copy them to your array. You'll trade some space for a guarantee on the time bounds.

一个更好的解决方案(不幸的是只解决了第二点)可能是生成一个你想要生成的不同数字的序列(10数字),随机排列这个序列,然后只选择该序列的第一个size元素并将它们复制到你的数组. 你会用一些空间换取时间范围内的保证。

int max_number = 10;
int[] all_numbers = new int[max_number];
for (int i = 0; i < max_number; i++)
    all_numbers[i] = i;

/* randomly permute the sequence */
for (int i = max_number - 1; i >= 0; i--) {
    int j = (int)(Math.random() * i); /* pick a random number up to i */

    /* interchange the last element with the picked-up index */
    int tmp = all_numbers[j];
    all_numbers[j] = a[i];
    all_numbers[i] = tmp;
}

/* get the a array */
for (int i = 0; i < size; i++)
    a[i] = all_numbers[i];

Or, you can create an ArrayListwith the same numbers and instead of the middle loop you can call Collections.shuffle()on it. Then you'd still need the third loop to get elements into a.

或者,您可以ArrayList使用相同的数字创建一个,而不是中间循环,您可以调用Collections.shuffle()它。然后你仍然需要第三个循环来将元素放入a.

回答by Karthik T

It might work out faster to start with a sequential arrayand shuffleit. Then they will all be unique by definition.

顺序数组开始并对其进行洗牌可能会更快。那么它们在定义上都是独一无二的

Take a look at Random shuffling of an array, and at the Collections.shuffle function.

看一看Random shuffle of an array和 Collections.shuffle 函数。

int [] arr = [1,2,3,.....(size)]; //this is pseudo code

Collections.shuffle(arr);// you probably need to convert it to list first

回答by balajeerc

If you just don't want to pay for the added overhead to ArrayList, you can just use an array and use Knuth shuffle:

如果您只是不想为 ArrayList 的额外开销付费,您可以只使用一个数组并使用Knuth shuffle

public Integer[] generateUnsortedIntegerArray(int numElements){
    // Generate an array of integers
    Integer[] randomInts = new Integer[numElements];
    for(int i = 0; i < numElements; ++i){
        randomInts[i] = i;
    }
    // Do the Knuth shuffle
    for(int i = 0; i < numElements; ++i){
        int randomIndex = (int)Math.floor(Math.random() * (i + 1));
        Integer temp = randomInts[i];
        randomInts[i] = randomInts[randomIndex];
        randomInts[randomIndex] = temp;
    }
    return randomInts;
}

The above code produces numElements consecutive integers, without duplication in a uniformly random shuffled order.

上面的代码生成 numElements 个连续整数,没有以均匀随机打乱顺序重复。

回答by Sachin Choudhary

 import java.util.Scanner;
 class Unique
{
public static void main(String[]args)
{
    int i,j;
    Scanner in=new Scanner(System.in);
    int[] a=new int[10];
    System.out.println("Here's a unique no.!!!!!!");
    for(i=0;i<10;i++)
    {
        a[i]=(int)(Math.random()*10);
        for(j=0;j<i;j++)
        {
            if(a[i]==a[j])
            {
                i--;

            }
        }   
    }
    for(i=0;i<10;i++)
    {
        System.out.print(a[i]);
    }
}
}

回答by akki

Input your size and get list of random unique numbers using Collections.

使用 Collections 输入您的尺寸并获取随机唯一数字列表。

public static ArrayList<Integer> noRepeatShuffleList(int size) {
    ArrayList<Integer> arr = new ArrayList<>();
    for (int i = 0; i < size; i++) {
        arr.add(i);
    }
    Collections.shuffle(arr);
    return arr;
}

Elaborating Karthik's answer.

详细说明 Karthik 的回答。

回答by Tamer M. Abu Rass

int[] a = new int[20];

for (int i = 0; i < size; i++) {
    a[i] = (int) (Math.random() * 20);

    for (int j = 0; j < i; j++) {
        if (a[i] == a[j]) {
            a[i] = (int) (Math.random() * 20); //What's this! Another random number!
            i--;
            break;
        }
    }
}

回答by David Mancera

    int[] a = new int [size];

    for (int i = 0; i < size; i++) 
    {
        a[i] = (int)(Math.random()*16); //numbers from 0-15
        for (int j = 0; j < i; j++) 
        {
            //Instead of the if, while verifies that all the elements are different with the help of j=0
            while (a[i] == a[j])
            {
                a[i] = (int)(Math.random()*16); //numbers from 0-15
                j=0;
            }
        }
    }

    for (int i = 0; i < a.length; i++)
    {
        System.out.println(i + ".   " + a[i]);
    }