生成随机数对,不重复

Generate random pairs of numbers, without duplicates

我必须使用整数数组:

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

我想生成一个数组,其中包含 a 和 b 数组中的对,顺序是随机的,没有重复项。例如我想得到以下结果:

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

谢谢!

下面是一些代码,它根据您输入的 a[]b[] 数组创建 10 个随机对,并将它们存储到 HashSet 中,您可以在以后根据需要使用它。 HashSet 将自动删除重复的对。

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

我想应该是这样的。我在 Pair class 和 main 的末尾添加了一个 toString 这样你就可以看到输出

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

    }

}

如果随机数组对你来说足够好,这是 IMO 最简单的方法:只需创建所有可能的对并将结果打乱,只要 ab 包含唯一值。

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

如果您想生成比可能的对总数更少的对,可以使用更有效的方法生成所有对,然后只生成前几个。如果是,请写在评论中。

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