Home > Backend Development > PHP Tutorial > How to get random numbers in PHP

How to get random numbers in PHP

墨辰丷
Release: 2023-03-27 10:14:02
Original
2077 people have browsed it

This article mainly introduces the common methods of simply obtaining random numbers in PHP, and analyzes the simple implementation techniques of PHP to implement random numbers in a specified range and random numbers in a specified character sequence in the form of examples. Friends in need can refer to the following

1. Directly obtain the number from min-max, such as 1-20:

$randnum = mt_rand(1, 20);
Copy after login

2. Randomly select one in an array ( The verification code requires a mixture of letters and numbers)

function randUid(){
 $str = "1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20";//要显示的字符,可自己进行增删
 $list = explode(",", $str);
 $cmax = count($list) - 1;
 $randnum = mt_rand(0, $cmax);
 $uid = $list[$randnum];
}
Copy after login

##Related recommendations:

php generatesrandom numberA string of words, letters or numbers mixed with letters

What PHP generatesRandom numbersMethod

JS generates a specified rangeRandom Detailed explanation of number and random sequence methods

##

The above is the detailed content of How to get random numbers in PHP. 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