如何在不重复的情况下随机生成两个数字?

时间:2022-11-25 12:58:26

I am just wondering how should I generate two numbers in java that are different from each other.

我只是想知道如何在java中生成两个彼此不同的数字。

I want to choose two numbers between 1-3 randomly, however, I don't want them to be the same numbers?

我想随机选择1-3之间的两个数字,但是,我不希望它们是相同的数字?

how should I do that? I tried to use a loop and I keep getting the same numbers

我该怎么做?我试图使用一个循环,我不断得到相同的数字

this is what I did

这就是我所做的

 Random random = new Random();

 for(int i =0; i<3; i++){
      int randomInteger = random.nextInt();
      System.out.println("Random Integer in Java: " + randomInteger);
 }

5 个解决方案

#1


1  

The solution depends on the scale you will be using. You can either generate numbers until you get the required results, which is in your case the better way to go, or you can "pick" random elements of a number set if the contents are known in advance.

解决方案取决于您将使用的规模。您可以生成数字,直到获得所需的结果,这在您的情况下是更好的方法,或者如果事先知道内容,您可以“选择”数字集的随机元素。

Generating until you get your results, with the use of while rejection:

生成直到获得结果,同时使用拒绝:

Random random = new Random();

int first = random.nextInt(3)+1;
int second;

while(second == null || first == second){
    second = random.nextInt(3)+1;
}

If you need more than two numbers from a larger set, you might consider using an array or a list as the result. It's easier to check if the result all ready occurred.

如果您需要更大的集合中的两个以上的数字,则可以考虑使用数组或列表作为结果。检查结果是否全部准备就更容易了。

The other approach is to pick numbers from a shuffled set.

另一种方法是从洗牌集中选择数字。

List<Integer> numberSet = new ArrayList<>();
for(int i = 1; i<=3; i++){
    numberSet.add(i);
}
Collections.shuffle(numberSet);

for(int j = 0; j<2; j++)
{
    System.out.println(numberSet.get(j));
}

In some cases you can save a couple of cycles this way.

在某些情况下,您可以通过这种方式节省几个周期。

#2


1  

following solution uses "virtual" removing of the first number, without using list or set. It is also optimized for given range and gets both values by single call to Random.nextInt().

以下解决方案使用“虚拟”删除第一个数字,而不使用列表或集合。它还针对给定范围进行了优化,并通过单次调用Random.nextInt()获取两个值。

    Random rand = new Random();
    for (int k=0; k<10; k++) {
        int v12=rand.nextInt(6);
        int v1=v12>>1;
        int v2=v12 & 0x1;
        if (v2>=v1) v2++;
        v1++; v2++; // from 0..2 to 1..3
        System.out.println("" + v1+" "+v2);
    }

#3


0  

Well the code is picking integers randomly but it's printing them at the same time without checking for any duplicates. In addition, it's not specifying that it has to be between 1-3

那么代码是随机选取整数,但它同时打印它们而不检查任何重复。此外,它没有指定它必须在1-3之间

So here is how it can be done.

所以这是如何做到的。

Of course you'll need to import java.util.Random first.

当然,您首先需要导入java.util.Random。

Random rand = new Random();
int value = rand.nextInt(3) +1;  // this will choose a number between 1-3 not 0-2
int secondvalue = rand.nextInt(3) +1; // same thing but we need 2 of them since we want 2 random numbers and not only 1 !

while(value == secondvalue) { //to see if the first random number = the second 
    secondvalue = rand.nextInt(3) +1; // if so, regenerate the second randomly
}

// and finally print them
System.out.println(" Value is " + value);
System.out.println(" Value is " + second value);

There are tons of ways to do this thing but I believe this is the easiest way to deal with it.

有很多方法可以做到这一点,但我相信这是处理它的最简单方法。

#4


0  

Random random = new Random();

Set<Integer> twoSet = new HashSet<>();

while (twoSet.size() < 2) {
    twoSet.add(random.nextInt(someNumber);
}

Take notice that if your range is too small this will loop endlessly.

请注意,如果你的范围太小,这将无休止地循环。

Also, you might want to take a look at this: http://en.wikipedia.org/wiki/Reservoir_sampling

另外,您可能需要查看此内容:http://en.wikipedia.org/wiki/Reservoir_sampling

#5


0  

Since there are only 6 combinations of possible answers, they can be brought together in a table, and then randomly retrieved from that table:

由于只有6种可能的答案组合,因此可以将它们放在一个表中,然后从该表中随机检索:

static int[] v1s=new int[]{1,1,2,2,3,3};
static int[] v2s=new int[]{2,3,1,3,1,2};

    Random rand = new Random();
    for (int k=0; k<10; k++) {
        int v12=rand.nextInt(6);
        int v1=v1s[v12];
        int v2=v2s[v12];
        System.out.println("" + v1+" "+v2);
    }

#1


1  

The solution depends on the scale you will be using. You can either generate numbers until you get the required results, which is in your case the better way to go, or you can "pick" random elements of a number set if the contents are known in advance.

解决方案取决于您将使用的规模。您可以生成数字,直到获得所需的结果,这在您的情况下是更好的方法,或者如果事先知道内容,您可以“选择”数字集的随机元素。

Generating until you get your results, with the use of while rejection:

生成直到获得结果,同时使用拒绝:

Random random = new Random();

int first = random.nextInt(3)+1;
int second;

while(second == null || first == second){
    second = random.nextInt(3)+1;
}

If you need more than two numbers from a larger set, you might consider using an array or a list as the result. It's easier to check if the result all ready occurred.

如果您需要更大的集合中的两个以上的数字,则可以考虑使用数组或列表作为结果。检查结果是否全部准备就更容易了。

The other approach is to pick numbers from a shuffled set.

另一种方法是从洗牌集中选择数字。

List<Integer> numberSet = new ArrayList<>();
for(int i = 1; i<=3; i++){
    numberSet.add(i);
}
Collections.shuffle(numberSet);

for(int j = 0; j<2; j++)
{
    System.out.println(numberSet.get(j));
}

In some cases you can save a couple of cycles this way.

在某些情况下,您可以通过这种方式节省几个周期。

#2


1  

following solution uses "virtual" removing of the first number, without using list or set. It is also optimized for given range and gets both values by single call to Random.nextInt().

以下解决方案使用“虚拟”删除第一个数字,而不使用列表或集合。它还针对给定范围进行了优化,并通过单次调用Random.nextInt()获取两个值。

    Random rand = new Random();
    for (int k=0; k<10; k++) {
        int v12=rand.nextInt(6);
        int v1=v12>>1;
        int v2=v12 & 0x1;
        if (v2>=v1) v2++;
        v1++; v2++; // from 0..2 to 1..3
        System.out.println("" + v1+" "+v2);
    }

#3


0  

Well the code is picking integers randomly but it's printing them at the same time without checking for any duplicates. In addition, it's not specifying that it has to be between 1-3

那么代码是随机选取整数,但它同时打印它们而不检查任何重复。此外,它没有指定它必须在1-3之间

So here is how it can be done.

所以这是如何做到的。

Of course you'll need to import java.util.Random first.

当然,您首先需要导入java.util.Random。

Random rand = new Random();
int value = rand.nextInt(3) +1;  // this will choose a number between 1-3 not 0-2
int secondvalue = rand.nextInt(3) +1; // same thing but we need 2 of them since we want 2 random numbers and not only 1 !

while(value == secondvalue) { //to see if the first random number = the second 
    secondvalue = rand.nextInt(3) +1; // if so, regenerate the second randomly
}

// and finally print them
System.out.println(" Value is " + value);
System.out.println(" Value is " + second value);

There are tons of ways to do this thing but I believe this is the easiest way to deal with it.

有很多方法可以做到这一点,但我相信这是处理它的最简单方法。

#4


0  

Random random = new Random();

Set<Integer> twoSet = new HashSet<>();

while (twoSet.size() < 2) {
    twoSet.add(random.nextInt(someNumber);
}

Take notice that if your range is too small this will loop endlessly.

请注意,如果你的范围太小,这将无休止地循环。

Also, you might want to take a look at this: http://en.wikipedia.org/wiki/Reservoir_sampling

另外,您可能需要查看此内容:http://en.wikipedia.org/wiki/Reservoir_sampling

#5


0  

Since there are only 6 combinations of possible answers, they can be brought together in a table, and then randomly retrieved from that table:

由于只有6种可能的答案组合,因此可以将它们放在一个表中,然后从该表中随机检索:

static int[] v1s=new int[]{1,1,2,2,3,3};
static int[] v2s=new int[]{2,3,1,3,1,2};

    Random rand = new Random();
    for (int k=0; k<10; k++) {
        int v12=rand.nextInt(6);
        int v1=v1s[v12];
        int v2=v2s[v12];
        System.out.println("" + v1+" "+v2);
    }