PHP rand random integer function



 The function of PHP rand function is to generate a random integer. We usually use the smallest integer and the largest integer in the rand function as the selection interval of random integers, such as the interval between 0 and 100, so that the rand function can choose randomly One of the numbers is shown, but the rand function does not force the PHP designer to make a clear mark on the randomly selected number range, which means that the PHP rand function can be generated within the preset range of the operating system without setting parameters. A random integer.


Basic syntax of PHP rand function
int rand ( void );
int rand (int $ min, int $ max);
There are two common ways to write the PHP rand function. The first is the one mentioned in the previous paragraph. There is no need to set parameters. Let rand generate a random integer according to the operating system's limit range. For Windows operating systems, the rand function will be at 0. Take a random integer between ~32767. What if you want to exceed this range? Then specify the min and max interval yourself! Then we use examples to show the actual operation results of the PHP rand function.

If the $max parameter is not set, the rule of rand is to automatically preset the $min parameter to 0, and the $max parameter depends on the operating system.

Practical example of PHP rand function
<?php
echo'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';

echo'Retrieve an integer in the restricted range of the Windows operating system:'.rand( );
echo'<br>';
echo'Get an integer in the range 0~100:'.rand(0,100);
echo'<br>'; echo'Get an integer
in the range -100~-1 :'.rand(-100,-1);
?>
Example output
Take an integer from the restricted range of the Windows operating system: 28087 Take an integer
from the range 0~100: 32 Take an integer
from the range -100~-1: -66
Since the example uses rand to fetch random integers, the sample program will almost have different results every time it runs. This is the real purpose of the PHP rand function. The first use of the rand function does not set the selection interval, so according to the Windows system Limit, let the rand function automatically take out an integer from 0 to 32767. The second use of rand sets the selection interval. This is no problem. The interesting thing is that the third use of the rand function, we let the rand function pick one of the negative numbers Integers are also possible.

Post a Comment

0 Comments