$rand = str_pad(dechex(rand(0x000000, 0xFFFFFF)), 6, 0, STR_PAD_LEFT); echo('#' . $rand); You can change randrand() in for mt_randmt_rand() if you want, and you can put strtoupper()strtoupper() around the dechexstr_pad() 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 :)