0

I have this class and it is working fine for me. It give 5 digit random number. What I can´t achieve is that the 5 numbers be different from each other, I mean no repeat it number in the 5 digit.

import java.util.Random;

public class Test
{

    public int[] dedo()
    {
        Random diceRoller = new Random();
        int[] cifra = new int[5];
        for (int i = 0; i < cifra.length; i++)
        {
            int roll = diceRoller.nextInt(9);
            cifra[i] = roll;
            System.out.print(roll);
        }
        return cifra;
    }
}
syb0rg
  • 8,057
  • 9
  • 41
  • 81

1 Answers1

1

Its not really random if you constrain the result like this, but a quick nasty way to do this would be with Collections.shuffle()

List<Integer> digits = Arrays.asList(0,1,2,3,4,5,6,7,8,9);
Collections.shuffle(digits);
return digits.subList(0, 4).toArray();
Zutty
  • 5,357
  • 26
  • 31