The first of these methods
Copy Code code as follows:
<?php
$FileID =date ("Ymd-his"). '-' . Rand (100,999);
$FileID a random number such as 20100903-132121-908.
?>
The second method
Copy Code code as follows:
<?php
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
Copy Code code as follows:
<?php
Seed user-defined functions in microseconds as seeds
function seed ()
{
List ($msec, $sec) = Explode (', microtime ());
return (float) $sec;
}
Sowing the seed of random number generator, using the Srand function to call the return result of the seed function
Srand (Seed ());
The random number produced by the output, the range of the random number is 10-100
echo rand (10,100);
?>
Does this have nothing to do with the bottom one? are random output between 10-100 of the number of new learning, may ask too simple OH
Copy Code code as follows:
<?php
echo rand (10,100);
?>
Mt_rand (10,100);
Srand is a seed, if not set, the default is 1.
Rand is usually a fixed operation that uses seeds as a parameter.
If you try, you'll know, no seed or set a fixed seed, run Rand
Then turn off the browser and run the rand again.
You'll find the results are always the same.
Let's say the rand () function, rand ([int min], [int max])
This function takes a random number from 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 the rand () function is used, the random number is a large disorder, and it is best to use the Srand () function to configure the new random number seed each time before taking the random number.
explain the following usage (which is generally the case with these two functions):
Srand (Double) microtime () *1000000);
$rand _number= rand ();
Microtime () returned two values: the current millisecond and timestamp, we want to extract the random number, we can only take a random number from the millisecond, (double) microtime () only returns the current millisecond value.
Microtime () is the number of milliseconds in seconds, so values are decimal, multiplied by 1000000 to convert them to integers
Their workflow is as follows:
(1): First, provide a "seed" to Srand (), which is a unsigned_int type value.
(2): _ Then, invoke rand (), which returns a random number based on the value provided to Srand () (range between _0~32767)
(3): To call rand () multiple times as needed, to get new random numbers.
(4): Whenever the Srand () can be given a new "seed", thereby further "randomization" of Rand ()
Output results.