Random Hex Color

Technique #1

    $rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
    $color = '#'.$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)].$rand[rand(0,15)];

Then echo out the $color value anywhere you need it. For example:

<body style="background: <?php echo $color; ?>;">

Technique #2

<?php printf( "#%06X\n", mt_rand( 0, 0xFFFFFF )); ?>

There is also a JavaScript version.


  1. User Avatar
    Permalink to comment#

    Simpler version:

    <php $color = sprintf(“#%06x”,rand(0,16777215)); ?>

    <body style="background: <php echo $color; ?>;”>

  2. User Avatar
    Starla Insigna

    Interestingly enough, I had to do this exact task just a few days ago. Here’s what I came up with:

    $color = str_pad(dechex(rand(0,16777215),6, "0", STR_PAD_LEFT);

  3. User Avatar
    Santosh Kumar
    Permalink to comment#

    In technique #1 you can tweak with the code to get Red, Green and Blue shades out of the code. The all you have to play with is the value upto 15.

  4. User Avatar
    Permalink to comment#

    Can I randomize hex colours from a preset list of colour hexes? If so then how?

    • User Avatar
      Permalink to comment#

      yes you can with #1, but your preset list must have some common number such as #axbxcx or #aabbxx

  5. User Avatar
    Permalink to comment#

    for the solution #1, you can actually contorl the random color where #2 cannot

  6. User Avatar
    Permalink to comment#

    How about this one?

    $rand = dechex(rand(0x000000, 0xFFFFFF));
    echo('#' . $rand);

    You can change rand in for mt_rand if you want, and you can put strtoupper() around the dechex to make the random number look nicer (although it’s not required). That would look like $rand = strtoupper(dechex(rand(0x000000, 0xFFFFFF)));

    It works perfectly and is way simpler than all the other methods described here :)

    • User Avatar

      I used the following line and refreshed my page many times (probably at least 30) and noticed that for it being ‘random’ it always seems to end in 00… I don’t think I had a single occurrence where it hasn’t yet.

      $rand = strtoupper(dechex(rand(0x000000, 0xFFFFFF)));

  7. User Avatar
    Permalink to comment#

    It can be nice to have control of the 3 component colours in case you don’t want a completely random colour from 000 to fff.
    $random_color = sprintf("%02x%02x%02x", mt_rand(0x22, 0xaa), mt_rand(0x22, 0xaa), mt_rand(0x22, 0xaa));
    Tweak each mt_rand to be the range of colours you want. Here I’ve chosen 22 to aa so the result is not too saturated.

  8. User Avatar
    Catatan Belajar
    Permalink to comment#

    The last snippet is very interesting :). Thanks.

  9. User Avatar
    Permalink to comment#

    Hi! Thanks.
    I tried all these options on this game http://lincygame.plenicus.com. Each solution gives different levels.

    Phil’s snippet is really good. :)

Leave a Comment

Posting Code!

You may write comments in Markdown. This makes code easy to post, as you can write inline code like `<div>this</div>` or multiline blocks of code in triple backtick fences (```) with double new lines before and after.

Code of Conduct

Absolutely anyone is welcome to submit a comment here. But not all comments will be posted. Think of it like writing a letter to the editor. All submitted comments will be read, but not all published. Published comments will be on-topic, helpful, and further the discussion or debate.

Want to tell us something privately?

Feel free to use our contact form. That's a great place to let us know about typos or anything off-topic.