note 69917 deleted from function.mt-rand by salathe

From: Date: Fri, 07 Nov 2014 11:01:35 +0000
Subject: note 69917 deleted from function.mt-rand by salathe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-200813@lists.php.net to get a copy of this message
Note Submitter: pHp_n00b ---- <?php // Generates a max."6 char" Hex-Code function hexcode() { $min = hexdec("000000"); // result is 0 and sets the min-value for mt_rand $max = hexdec("FFFFFF"); // result is 16777215 and sets the max-value for mt_rand $random = mt_rand($min, $max); // creates a radom number between 0 and 16777215 $random_hex = dechex($random); // transforms the random number into a Hex-Code // now the test, if the result has 6 chars if (strlen($random_hex) != "6") // sometimes it returns an only 5, or less, char Hex-Code, hexcode(); // so the function has to be repeat else echo $random_hex; // returns the Hex-Code } hexcode(); ?>

« previous php.notes (#200813) next »