Home  >  Article  >  Backend Development  >  How to use php rand function

How to use php rand function

藏色散人
藏色散人Original
2019-05-23 14:49:3610442browse

php The rand() function is used to generate random integers within a specified range. The syntax is "rand(min,max)". The parameter min specifies the minimum number in the range, and the parameter max specifies the maximum number in the range; if not If the parameters are specified, a pseudo-random integer between 0 and RAND_MAX is returned.

How to use php rand function

How to use the rand() function?

Function: Get a random number from the parameter range

Syntax:

rand(min,max)

Parameter:

min, indicating the minimum number in the range

max, represents the maximum number in the range

Note: If the optional parameters min and max are not provided, rand() returns a pseudo-random integer between 0 and RAND_MAX. For example, if you want a random number between 5 and 15, inclusive, use rand(5, 15).

php rand() function example 1

<?php
$i = 1;
$j = 9;
$k = rand($i,$j);
echo "取得的随机数为".$k;
?>

Output:

随机输出1-9之间的一个整数

php rand() function example 2

<?php
echo rand(1,100);
?>

Output:

随机输出1-100之间的一个整数

The above is the detailed content of How to use php rand function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn