Home >Backend Development >PHP Tutorial >A simple common method for obtaining random numbers in PHP

A simple common method for obtaining random numbers in PHP

陈政宽~
陈政宽~Original
2017-06-28 13:39:211437browse

This article mainly introduces the common methods of PHP to simply obtain random numbers. Combined with examples, it analyzes the simple implementation techniques of PHP to implement random numbers in a specified range and random numbers in a specified character sequence. Friends who need it can Refer to the following

The example of this article describes the common method of simply obtaining random numbers in PHP. Share it with everyone for your reference, the details are as follows:

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

$randnum = mt_rand(1, 20);

2. In an array Choose one randomly (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];
}

The above is the detailed content of A simple common method for obtaining random numbers in PHP. 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