简体   繁体   English

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

[英]How should I randomly generate two numbers at the same time without them being duplicated?

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);
 }

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: 生成直到获得结果, while使用拒绝:

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. 在某些情况下,您可以通过这种方式节省几个周期。

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);
    }

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. 当然,您首先需要import 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. 有很多方法可以做到这一点,但我相信这是处理它的最简单方法。

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

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);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何生成随机数并将其随机分配给某些按钮? - How to generate random numbers and distribute them randomly to some buttons? 如何在Java中不重复的情况下随机生成两个给定整数之间的所有数字? - How to generate all numbers randomly between two given integers without duplication in Java? 如何从数组中随机生成数字,每个数字都是唯一的 - How to generate numbers from an array randomly,with each number being unique 如何在 Java 中随机生成 0 到 3 之间的整数而不使它们连续? - How to randomly generate integers between 0 to 3 without making them consecutive in Java? 如何在不使用数组的情况下生成随机数列表并确定最大数? - How should I generate a list of random numbers and determine the largest without using an array? 如何添加两个包含十六进制数字的字符串而不将它们转换为 int? - How can I add two Strings containing hexadecimal-numbers without converting them to int? 如何在2D平面上随机生成一堆网站,它们之间的空间大致相同? - How to randomly generate a bunch of sites on a 2D plane that have roughly the same amount of space between them? 随机生成特定数字 - Randomly Generate specific numbers 如何在不将它们相乘的情况下验证 3 个数字的乘积符号? - How can I verify the sign of product of 3 numbers without multiplying them? 我应该如何直接使用 UTF8 或 UTF16 随机生成一个固定长度的字符串变量? - How should I randomly generate a fixed length String variable directly using UTF8 or UTF16?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM