mercredi 17 février 2016

Java Random Color String

I have written this java method but sometimes the color String is only 5 characters long. Does anyone know why?

@Test
public void getRandomColorTest() {
    for (int i = 0; i < 20; i++) {
        final String s = getRandomColor();
        System.out.println("-> " + s);
    }
}

 public String getRandomColor() {
    final Random random = new Random();
    final String[] letters = "0123456789ABCDEF".split("");
    String color = "#";
    for (int i = 0; i < 6; i++) {
        color += letters[Math.round(random.nextFloat() * 15)];
    }
    return color;
}




Aucun commentaire:

Enregistrer un commentaire