I am trying to create pairs of random integers in the range [0,n) . I need to make sure that for any input n numbers created, for example, p, q, are such that p != q
I tried using java.util.Random with seed sothat, I can reproduce the result. I tried the inputs 100,200,400,800 and they all created p, q such that p !=q But in 1600, two pairs were with p == q
public static void generate(int size){ Random ran = new Random(); ran.setSeed(123456L); for(int i =0;i<size;i++){ int p = ran.nextInt(size); int q = ran.nextInt(size); if(p==q) System.out.println(p+" equals "+q);
it gave
692 equals 692 843 equals 843
I am sure there is some way to make sure p! = Q for any input n .. but I canβt remember the required math
Can anyone help?
damon source share