Generate random pairs of numbers, without duplicates

5.6k views Asked by At

I have to arrays with integers:

int[] a={1,2,3,4,5};
int[] b={6,7};

I would like to generate an array, which contains pairs from the a and b arrays, in a random order, without duplicates. For example I would like to get the following result:

c={(1,6),(2,7),(4,6),...}

Thanks!

4

There are 4 answers

3
Tim Biegeleisen On BEST ANSWER

Here is some code which creates 10 random pairs from your input a[] and b[] arrays, and stores them into an HashSet which you can use later as you see fit. The HashSet will automatically remove duplicate pairs.

public class Pair {
    int x;
    int y;

    public Pair(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Pair otherPair = (Pair) obj;
        if (this.getX() != otherPair.getX() || this.getY() != otherPair.getY()) {
            return false;
        }

        return true;
    }

    // getters and setters
}

public class PairTest {
    public static void main(String[] args) {
        Random randomGenerator = new Random();

        int[] a={1,2,3,4,5};
        int[] b={6,7};

        Set<Pair> pairs = new HashSet<Pair>();

        do {
            int xRand = randomGenerator.nextInt(a.length);
            int yRand = randomGenerator.nextInt(b.length);

            Pair p;
            if (xRand % 2 == 0) {
                Pair p = new Pair(a[xRand], b[yRand]);
            }
            else {
                Pair p = new Pair(b[yRand], a[xRand]);
            }
            pairs.add(p);
            if (pairs.size() == 10) break;
        } while (true);
    }
}
0
kajacx On

If an random array is good enough for you, this is IMO the most simple way: Just create all possible pairs and shuffle the result, that will grand randomness and uniqueness as long as a and b contain unique values.

public static void main(String[] args) {
    int[] a = { 1, 2, 3, 4, 5 };
    int[] b = { 6, 7 };

    ArrayList<Pair> pairs = new ArrayList<>();
    for (int i = 0; i < a.length; i++) {
        for (int j = 0; j < b.length; j++) {
            pairs.add(new Pair(a[i], b[j]));
        }
    }

    Collections.shuffle(pairs);

    Pair[] asArray = pairs.toArray(new Pair[0]); //if you prefer array over ArrayList
}

class Pair {
    int a, b;

    public Pair(int a, int b) {
        this.a = a;
        this.b = b;
    }
}

If you would like to generate less pairs that the total amount of pairs possible, there are more effective ways then to generate them all and then just take the first few. If so, write in the comments.

0
AudioBubble On
    int[] a={1,2,3,4,5};
    int[] b={6,7};
    List<int[]> list = new ArrayList<>();
    for (int i = 0; i < a.length; ++i)
        for (int j = 0; j < b.length; ++j)
            list.add(new int[] {a[i], b[j]});
    Collections.shuffle(list);
4
Nagarz On

Something like this would do I guess. I added a toString to the Pair class and at the end of the main so you can see the output

import java.util.ArrayList;
import java.util.Collections;

class Pair {

    private Integer a;
    private Integer b;

    public Pair(Integer a, Integer b) {
        super();
        this.a = a;
        this.b = b;
    }

    public Integer getA() {
        return a;
    }
    public void setA(Integer a) {
        this.a = a;
    }
    public Integer getB() {
        return b;
    }
    public void setB(Integer b) {
        this.b = b;
    }

    @Override
    public String toString() {
        return "Pair [a=" + a + ", b=" + b + "]";
    }


}

public class MainTest {

    public static void main(String[] args) {
        ArrayList<Pair> pairs = new ArrayList<Pair>();

        int[] a={1,2,3,4,5};
        int[] b={6,7};

        for (int i = 0; i<a.length; i++) {
            pairs.add(new Pair(a[i], b[(int) Math.round(Math.random())]));
        }
        Collections.shuffle(pairs);
        System.out.println(pairs);

    }

}