Today we learn how to generate a random number in PHP. In PHP, following function are used to generate random numbers.
rand():
rand() function is used to display a random number. Syntax:
rand();
Or you can also write the syntax as follows:
rand(min,max);
In above function two parameter are there:
- min: It is a optional parameter. min is the minimum number that is returned by the function. Default value is 0.
- max: max is also an optional parameter. It is the maximum value of number that is returned by the function. Default value is getrandmax().
mt_rand():
mt_rand() function is also used to display the random number with the help of Mersenne Twister algorithm. It execute 4 times faster than the rand() function and also produce better random number. Syntax:
mt_rand();
or you can write the syntax as follows:
mt_rand(min,max);
In above function two parameter are there:
- min: It is a optional parameter. min is the minimum number that is returned by the function. Default value is 0.
- max: max is also an optional parameter. It is the maximum value of number that is returned by the function. Default value is getrandmax().
0 Comment(s)