mercredi 21 novembre 2018

Random code of colors(jawa.awt) without duplicates

So i'm trying to make a random code that uses jawa.awt.colors. I want the code not be able to have duplicates, like it does atm. The code needs to hold 4 colors.

I found that you can use java.util.Collections.shuffle(), but this unfortunately doesn't work with with java.awt colors and i need to use the java.awt.colors later in my code so i can't just use something else.

Is there any other way?

Below is my code that does make duplicates:

public class SecretCombination {

public int codeLength = 4;

public Random random = new Random();

public Color[] PossibleColors = {Color.RED, Color.green, Color.blue, Color.yellow, Color.PINK,Color.BLACK};

public Color[] SecretColorCombi = new Color[codeLength];


public SecretCombination() {
    for (int i = 0; i < codeLength; i++) {
        SecretColorCombi[i] = PossibleColors[random.nextInt(PossibleColors.length)];
    }
}

}




Aucun commentaire:

Enregistrer un commentaire