Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,844 questions

55,671 answers

573 users

How to generate a random color in RGB format with Java

2 Answers

0 votes
import java.util.Random;

public class RandomRGBColor {

    // Function to generate a random number between min and max (inclusive)
    public static int generateRandomNumber(int min, int max) {
        Random rand = new Random();
        
        return rand.nextInt(max - min + 1) + min;
    }

    // Function to generate a random RGB color
    public static void generateRandomRGBColor() {
        int red = generateRandomNumber(0, 255);
        int green = generateRandomNumber(0, 255);
        int blue = generateRandomNumber(0, 255);

        System.out.println("Random RGB Color: rgb(" + red + ", " + green + ", " + blue + ")");
    }

    public static void main(String[] args) {
        generateRandomRGBColor();
    }
}



/*
run:

Random RGB Color: rgb(25, 169, 68)

*/

 



answered Oct 9, 2025 by avibootz
0 votes
/**
    Generate a random color in RGB format: rgb(R, G, B)
    This program demonstrates how numbers and bits are used
    to produce valid 8‑bit channel values.
*/

import java.util.Random;

public class Main {

    /**
     * Create a random 8‑bit integer (0–255).
     * Random.nextInt(256) returns a value in [0, 256),
     * giving exactly one byte of color data.
     */
    public static int randomChannel(Random rng) {
        // 8 bits → values from 0 to 255
        return rng.nextInt(256);
    }

    /**
     * Produce a random RGB color by combining the channels.
     */
    public static RGBResult generateRandomRGB(Random rng) {
        int r = randomChannel(rng);  // Red channel (8 bits)
        int g = randomChannel(rng);  // Green channel (8 bits)
        int b = randomChannel(rng);  // Blue channel (8 bits)

        // Construct the CSS-style RGB string
        String rgb = "rgb(" + r + ", " + g + ", " + b + ")";

        return new RGBResult(r, g, b, rgb);
    }

    // Simple container for the result
    public static class RGBResult {
        public final int r;
        public final int g;
        public final int b;
        public final String rgb;

        public RGBResult(int r, int g, int b, String rgb) {
            this.r = r;
            this.g = g;
            this.b = b;
            this.rgb = rgb;
        }
    }

    public static void main(String[] args) {
        Random rng = new Random();
        RGBResult result = generateRandomRGB(rng);

        System.out.println("Red (8 bits): " + result.r);
        System.out.println("Green (8 bits): " + result.g);
        System.out.println("Blue (8 bits): " + result.b);
        System.out.println("RGB color: " + result.rgb);
    }
}


/*
run:

Red (8 bits): 57
Green (8 bits): 201
Blue (8 bits): 144
RGB color: rgb(57, 201, 144)

*/

 



answered 1 day ago by avibootz
...