How to use php rand function

藏色散人
Release: 2023-04-06 19:06:02
Original
10396 people have browsed it

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)
Copy after login

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;
?>
Copy after login

Output:

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

php rand() function example 2

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

Output:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!