public class Testrandom {
public static void Main (string[] args) {
Random number
int[] random = new int[4];
for (int i = 0; i < random.length; i++) {
Random[i] = (int) (Math.random () * 10);
for (Int J =0; J < I; J + +) {
if (random[j] = = Random[i]) {
i--;
Break
}
}
}
for (int i = 0; i < random.length; i++) {
System.out.print (Random[i]);
}
Analysis: To enter a random four-digit number is not too much difficulty, only need each random[i]= (int) (Math.random () *10); However, the resulting four-digit number is a certain probability of generating duplicate numbers, the code in the red number part will random[i] and the number generated before the comparison, if there is equal, i--, jump out of the loop, re-regenerated into a random[i], so reciprocating. Generates a random four-digit number without repeating numbers
Generate a random number without repeating numbers (JAVA)