简体   繁体   中英

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.

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

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:

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 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

So here is how it can be done.

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM