mercredi 18 novembre 2015

Random characters and random colors generator

public static char randomLetter() {
    Random r = new Random();
    char randomChar = (char) (97 + r.nextInt(26));
    return randomChar;
}

public static Color randomColor(){
    Random rand = new Random();
    float r = rand.nextFloat();
    float g = rand.nextFloat(); 
    float b = rand.nextFloat();
    Color randomColor = new Color(r, g, b);
    return randomColor;
}

I have these two methods. One returns a random letter in the alphabet and the other returns a random color. How do I edit these methods so that randomLetter() only returns a letter once and not return two of the same letters?

How do I make my randomColor() only return random colors between RED, YELLOW, GREEN, or BLUE?




Aucun commentaire:

Enregistrer un commentaire