Random color

Generates a random hex color (like FF00FF).

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



random_color() => returns something like: '7C42BA', '5F3964'

Sorry folks, comments have been deactivated for now due to the large amount of spam.

Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!


Older comments:

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