How to generate random numbers with non-repeating digits in Java? -


this question has answer here:

when using random utility in java, numbers this: 1271, 34556, 177, etc... simplest way make digits don't repeat?

how creating loop checks if generated random number meets requirements.

final random rnd = new random(); int n; {     n = rnd.nextint(1000); } while(containsrepeatingdigits(n)); 

containsrepeatingdigits can this:

boolean containsrepeatingdigits(final int n) {     final boolean digits[] = new boolean[10];     for(char c : string.valueof(n).tochararray()) {         final int = c-'0';         if(digits[i])             return true;         digits[i] = true;     }     return false; } 

Comments