This question already has an answer here:
- Generating Unique Random Numbers in Java 12 answers
i was using the code below to random pick 3 digit from 0~9, but some of the times it will generate 2 or 3 same number(like 1,1,2 or 8,8,8), can i prevent it? i just want it like 1,2,3.
Random random = new Random();
int[] sequence = new int[3];
for (int i = 0; i < 3; i++)
sequence[i] = Math.abs(random.nextInt(9));
System.out.println("Input: ");
for (int i = 0; i < 3; i++)
System.out.print(sequence[i] + " ");
Aucun commentaire:
Enregistrer un commentaire