PHP get random numbers and letters method Daquan
The first of these methods
$FileID =date ("Ymd-his"). '-' . Rand (100,999);
$FileID a random number such as 20100903-132121-908.
?>
The second method of
function Randomkeys ($length) {
$RETURNSTR = ";
$pattern = ' 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLOMNOPQRSTUVWXYZ ';
for ($i = 0; $i < $length; $i + +) {
$returnStr. = $pattern {Mt_rand (0, 61)}; Generate PHP Random number
}
return $returnStr;
}
Echo Randomkeys (4);
?>
The third method of
Seed user-defined functions with microseconds as seeds
function seed ()
{
List ($msec, $sec) = explode (' ', Microtime ());
return (float) $sec;
}
Seeding the random number generator and invoking the return result of the seed function with the Srand function
Srand (Seed ());
The output generates random numbers, and the range of random numbers is 10-100.
echo rand (10,100);
?>
Isn't this the same as the one below? are random output 10-100 between the number of new students learning, may ask too simple OH
echo rand (10,100);
?>
Mt_rand (10,100);
Srand is a seed, if not set, the default is 1.
Rand is generally a fixed operation that uses seed to make parameters.
You'll find out if you try. Run Rand without seeding or setting a fixed seed
Then close the browser and open it again, and then run Rand.
You'll find the results are always the same.
First say the rand () function, rand ([int min], [int max]) This function takes a random number between Min and Max. Without specifying the maximum and minimum range of random numbers, this function automatically takes a random number from 0 to Rand_max.
However, if only rand () is used, the random number is very chaotic and it is best to use the Srand () function to configure a new random number seed each time before taking a random number.
Explain the following usage (which is generally the case with these two functions):
Srand (Double) microtime () *1000000);
$rand _number= rand ();
Microtime () returns two values: the current millisecond and timestamp, we want to extract the random number, only a random number from the millisecond, (double) microtime () only returns the current millisecond value.
Microtime () is the number of milliseconds in seconds, so the value is decimal, multiplied by 1000000 to convert it to an integer
Their workflow is as follows:
(1): First, give Srand () a "seed"; it is a value of type Unsigned_int.
(2): _ Then, call Rand (), which returns a random number (range between _0~32767) based on the value provided to Srand ()
(3): Call rand () as many times as needed, and get new random numbers continuously.
(4): Whenever you can provide a new "seed" for Srand (), further "Randomize" rand ()
Output results.