Three ways to generate random numbers in PHP
How do I generate a random number between 1-10 in PHP?
Example 1, use the shuffle function to generate a random number.
4 |
foreach ($arr as $values) |
Example 2, use the Array_unique function to generate a random number.
3 |
while (count ($arr) <10) |
6 |
$arr =array_unique ($arr); |
8 |
echo Implode ("", $arr); |
Example 3, using the Array_flip function to generate random numbers, you can remove duplicate values.
08 |
$return [] = Mt_rand (1, 10); |
09 |
$return = Array_flip (Array_flip ($return)); |
10 |
$count = count ($return); |
12 |
foreach ($return as $value) |
17 |
$arr =array_values ($return);//Get the value of the array |
18 |
foreach ($arr as $key) |
PHP Random number Generation Function Example
02 |
function RANDPW ($len =8, $format = ' all ') { |
03 |
$is _ABC = $is _numer = 0; |
07 |
$chars = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 '; |
10 |
$chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz '; |
13 |
$chars = ' 0123456789 '; |
16 |
$chars = ' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 '; |
19 |
Mt_srand (Double) Microtime () *1000000*getmypid ()); |
20 |
while (strlen ($password) < $len) { |
21st |
$tmp =substr ($chars, (Mt_rand ()%strlen ($chars)), 1); |
22 |
if (($is _numer <> 1 && is_numeric ($tmp) && $tmp > 0) | | $format = = ' CHAR ') { |
25 |
if ($is _abc <> 1 && preg_match ('/[a-za-z]/', $tmp)) | | $format = = ' number ') { |
30 |
if ($is _numer <> 1 | | $is _abc <> 1 | | empty ($password)) { |
31 |
$password = RANDPW ($len, $format); |
35 |
for ($i = 0; $i < $i + +) { |
36 |
Echo RANDPW (8, ' number '); |
http://www.bkjia.com/PHPjc/876453.html www.bkjia.com true http://www.bkjia.com/PHPjc/876453.html techarticle Three ways PHP generates random numbers how do I generate a random number between 1-10 in PHP? Example 1, use the shuffle function to generate a random number. 1. PHP 2 $arr =range (1,10); 3 Shuffle ($arr); 4 fo ...