Skip to content

Instantly share code, notes, and snippets.

@mwamodo
Last active December 21, 2022 20:39
Show Gist options
  • Save mwamodo/b755c97638f3d3f44e228e0597d85e8f to your computer and use it in GitHub Desktop.
Save mwamodo/b755c97638f3d3f44e228e0597d85e8f to your computer and use it in GitHub Desktop.
Generate Random Colors Java
import java.awt.Color;
import java.util.Random;

public class RandomColorGenerator {
  public static void main(String[] args) {
    Random random = new Random();

    // Generate random values for the red, green, and blue components of the color
    int r = random.nextInt(256);
    int g = random.nextInt(256);
    int b = random.nextInt(256);

    // Create a new Color object using the random values
    Color randomColor = new Color(r, g, b);
  }
}
import java.util.Random;

public class RandomWordGenerator {
    public static void main(String[] args) {
        // Create a list of words
        String[] words = {"apple", "banana", "cherry", "date", "elderberry", "fig", "grape", "huckleberry"};

        // Create a Random object
        Random random = new Random();

        // Generate a random index
        int index = random.nextInt(words.length);

        // Select a random word from the list
        String word = words[index];

        // Print the selected word
        System.out.println(word);
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment