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 HEX format with Java

2 Answers

0 votes
import java.util.Random;

public class RandomHexColor {
    public static String GenerateRandomHexColor() {
        String hexChars = "0123456789ABCDEF";
        StringBuilder hex = new StringBuilder();
        Random rand = new Random();

        for (int i = 0; i < 6; i++) {
            hex.append(hexChars.charAt(rand.nextInt(16)));
        }
        
        return hex.toString();
    }
    
    public static void main(String[] args) {
        
        System.out.println("Random HEX Color: #" + GenerateRandomHexColor());
    }
}



/*
run:

Random HEX Color: #905B6B

*/

 



answered Oct 9, 2025 by avibootz
edited 1 day ago by avibootz
0 votes
/**
    Generate a random color in HEX format (#RRGGBB).
    This program demonstrates how numbers and bits are used
    to produce a valid 24‑bit color value.
*/

import java.util.Random;

public class Main {

    /**
     * Create a 24‑bit random integer (0x000000–0xFFFFFF).
     * Random.nextInt(bound) returns a value in [0, bound),
     * so using 0x1000000 (2^24) gives exactly 24 bits.
     */
    public static int randomColorInt(Random rng) {
        // 24 bits → values from 0 to 16,777,215 (0xFFFFFF)
        return rng.nextInt(0x1000000);
    }

    /**
     * Convert a 24‑bit integer into a hex color string.
     * String.format("%06X") ensures exactly 6 uppercase hex digits.
     */
    public static String intToHexColor(int value) {
        String hex = String.format("%06X", value); // Convert to 6‑digit hex

        return "#" + hex;
    }

    /**
     * Produce a random hex color by combining the two functions.
     */
    public static ColorResult generateRandomHexColor(Random rng) {
        int value = randomColorInt(rng);       // 24‑bit random number
        String hex = intToHexColor(value);     // Convert to #RRGGBB

        return new ColorResult(value, hex);
    }

    // Simple record-like container for the result
    public static class ColorResult {
        public final int value;
        public final String hex;

        public ColorResult(int value, String hex) {
            this.value = value;
            this.hex = hex;
        }
    }

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

        System.out.println("Random 24-bit value: " + result.value);
        System.out.println("Hex color: " + result.hex);
    }
}



/*
run:

Random 24-bit value: 8137980
Hex color: #7C2CFC

*/

 



answered 1 day ago by avibootz
...