dimanche 1 novembre 2015

java random 3 input prevent output same [duplicate]

This question already has an answer here:

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