Php random number code. Php random number code today, a friend asked about the fire PHP random number problem, this is the basic problem of PHP, you can refer to the fire provided by the PHP Manual, due to the time relationship, php random digit code
Today, I have a friend who asked me about the random numbers of PHP. this is the basic problem of PHP. you can refer to the PHP manual provided by iron. due to the time relationship, the editor is also lazy, I have found some examples on the internet. I hope they will help you. if you have any questions, please refer to them below.
// Seed user-defined functions use microseconds as the seed
Function seed ()
{
List ($ msec, $ sec) = explode ('', microtime ());
Return (float) $ sec;
}
// Broadcast the seed of the random number generator and use the srand function to call the returned result of the seed function.
Srand (seed ());
// Random number generated by the output. the random number ranges from 10 to 100.
Echo rand (10,100 );
?>
Is the above and below no different? They all randomly output numbers between 10-. it's too easy for new people to learn.
Echo rand (10,100 );
?>
Mt_rand (10,100 );
Srand is the seed. if not set, the default value is 1.
Rand is a fixed operation of parameters using seeds.
You can give it a try. you don't need a seed or set a fixed seed to run rand.
Then turn off the browser, and then run rand.
You will find that the results are always the same
However, if a random number is used as the seed, the running result is similar to a random one.
Let's talk about the rand () function. rand ([int min], [int max]) gets a random number between min and max. If the maximum and minimum ranges of random numbers are not specified, this function automatically retrieves a random number from 0 to RAND_MAX.
However, if you only use the rand () function, the random number is messy. you 'd better use the srand () function before getting the random number each time to configure the new random number seed.
Explain the following usage of functions (these two functions are generally used in this way ):
Srand (double) microtime () * 1000000 );
$ Rand_number = rand ();
Microtime () returns two values: the current millisecond and timestamp. to extract a random number, we can only get one random number from the millisecond, (double) microtime () only the current millisecond value is returned.
Microtime () is the number of milliseconds in seconds. Therefore, the values are decimal places and multiplied by 1000000 to convert them into integers.
Their workflow is as follows:
(1): first, provide a "seed" to srand (), which is a value of the unsigned_int type.
(2): _ then, call rand (), which returns a random number (in the range of _ 0 ~) based on the value provided to srand ~ Between 32767)
(3): call rand () multiple times as needed to continuously obtain new random numbers.
(4): whenever srand () can be provided with a new "seed" to further "Randomize" rand ()
Output result.
Today, I have a friend who asked me about the random number of PHP. this is the basic problem of PHP. you can refer to the PHP manual provided by the fire. due to the time relationship, I will make a small Editor...