Random color

Generates a random hex color (like FF00FF).

Snippet information

Author:
Jonas John

License:
Public Domain

Language:
PHP

Created:
05/22/2006

Updated:
05/22/2006

Tags:
,


function random_color(){
    mt_srand((double)microtime()*1000000);
    $c = '';
    while(strlen($c)<6){
        $c .= sprintf("%02X", mt_rand(0, 255));
    }
    return $c;
}


Found a bug? Or do you have a better solution for this?
Feel free to leave a message:

Add a comment


Leave a comment

Darryll January 02, 2008 at 14:44
$colour = rand(0,10000000);
echo "Colour: ".dechex($colour);