java - Generate random pairs of numbers, without duplicates -


i have arrays integers:

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

i generate array, contains pairs , b arrays, in random order, without duplicates. example following result:

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

thanks!

here code creates 10 random pairs input a[] , b[] arrays, , stores them hashset can use later see fit. hashset 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 , 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>();          {             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);     } } 

Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -