Generate random data in PHP directly using Mt_rand can be achieved, if you want to generate non-repeating random number we can use the Unique_rand function, let me summarize the commonly used methods.
The code is as follows:
The code is as follows |
Copy Code |
Range is to list 1 to 100 as an array $numbers = range (1,100); Shuffle the array order is scrambled Shuffle ($numbers); Array_slice take a segment of the array $no = 6; $result = Array_slice ($numbers, 0, $no); for ($i =0; $i < $no; $i + +) { echo $result [$i]. " "; } Print_r ($result); ?> Range is to list 1 to 42 as an array $numbers = range (1,42); Shuffle the array order is scrambled Shuffle ($numbers); Array_slice take a segment of the array $result = Array_slice ($numbers, 0, 3); Print_r ($result);
|
Method 2
The code is as follows |
Copy Code |
$numbers = range (1,20); Srand (float) microtime () *1000000); Shuffle ($numbers); while (list (, $number) = each ($numbers)) { echo "$number"; } ?> |
Method 3
Using PHP, randomly generate 5 distinct values in 1-20, how to do
The code is as follows |
Copy Code |
function Norand ($begin =0, $end =20, $limit =5) { $rand _array=range ($begin, $end); Shuffle ($rand _array);//Call out-of-box array random permutation function Return Array_slice ($rand _array,0, $limit);//$limit before intercept } Print_r (Norand ()); ?> |
Or not shuffle.
The code is as follows |
Copy Code |
$tmp =array (); while (count ($tmp) <5) { $tmp []=mt_rand (1,20); $tmp =array_unique ($tmp); } Print join (', ', $tmp); ?> |
Above are on paper, the following is true, the requirements are as follows
There are 25 pieces to vote, one to vote for 16, and one to vote only once per piece. In front of a programmer stabbed blunder, forgot to put the vote in the library, there are 200 user-generated voting sequence is empty. So how would you fill this blunder?
Certainly reflect the situation to the superiors. But what we're talking about here is the technology that needs to generate 16 non-repeating random numbers between 1-25 to fill. How do you design the function specifically? A number of distinct random numbers can be generated by storing random numbers in an array and then removing duplicate values in the array.
code as follows |
copy code |
/* * Array Unique_rand (int $min, int $max, int $num) * Generate a certain number of non-repeating random numbers * $min and $max: Specify the range of random numbers * $num: Specify the number of builds */ function Unique_rand ($min, $max, $num) { $count = 0; $return = Array (); while ($count < $num) { $return [] = Mt_rand ($min, $max); $return = Array_flip (Array_flip ($return)); $count = count ($return); } Shuffle ($return); return $return; } $arr = Unique_rand (1, 25, 16); Sort ($arr); $result = "; for ($i =0; $i < count ($arr); $i + +) { $result. = $arr [$i]. ', '; } $result = substr ($result, 0,-1); echo $result; ?> The program runs as follows: 1 2,3,4,6,7,8,9,10,11,12,13,16,20,21,22,24 |
Add a few notes:
• The Mt_rand () function is used when generating random numbers. The average speed of this function generates random numbers four times times faster than Rand ().
• A "rollover" is used to remove duplicate values from the array, which is to swap the array's key and value two times with Array_flip (). This approach is much faster than using Array_unique ().
• Before returning an array, use Shuffle () to assign a new key name to the group, guaranteeing that the key name is 0-n consecutive digits. If you do not do this, you may cause the key name to be discontinuous when you delete duplicate values, causing the traversal to be cumbersome.
Look at an example again
Generates one of the 36 characters of the 0-z. Each call to the GetOptions () method generates a character that is stored as follows: array[0] = 0, array[1] = 1, ..., array[35] = Z.
code as follows |
copy code |
Array ( [0] = 0 [1] = 1 [2] = 2 [3] = 3 [4] = 4 [5] = 5 [6] + 6 [7] + 7 [8] + 8 [9] + = 9 [ten] + a [one] = b [ten] + C [all] + D [+]/-e [+] = f [+] = g [+] = h [+] = i [+] + j [+] + k [+] = l [[]] = M [P] = n [+] = o [+] = P [+] = q [+] = R [+] + s [29] =& Gt T [+] = u [+] = v [+] = w [[+] = x [+] = y [x] = Z ] |
Then randomly generate a number as an index between 0-35, which is the first character in the variable $result by randomly fetching a number in the array above. This random index is then assigned to the last of the array, and it will not participate in the next round of random selection.
The code is as follows |
Copy Code |
Generate one character in 0123456789abcdefghijklmnopqrstuvwxyz function GetOptions () { $options = Array (); $result = Array (); for ($i =48; $i <=57; $i + +) { Array_push ($options, Chr ($i)); } for ($i =65; $i <=90; $i + +) { $j = 32; $small = $i + $j; Array_push ($options, Chr ($small)); } return $options; } /* $e = GetOptions (); for ($j =0; $j <150; $j + +) { echo $e [$j]; } */ $len = 10; Randomly generate an array index to achieve random numbers for ($j =0; $j <100; $j + +) { $result = ""; $options = GetOptions (); $lastIndex = 35; while (strlen ($result) < $len) { Randomly take one as an index from 0 to 35 $index = rand (0, $lastIndex); Assigns a random number to a variable $CHR $CHR = $options [$index]; Random numbers as part of the $result $result. = $CHR; $lastIndex = $lastIndex-1; The last index will not participate in the next random draw $options [$index] = $options [$lastIndex]; } echo $result. " n "; } ?> |
http://www.bkjia.com/PHPjc/633165.html www.bkjia.com true http://www.bkjia.com/PHPjc/633165.html techarticle generate random data in PHP directly using Mt_rand can be achieved, if you want to generate non-repeating random number we can use the Unique_rand function, let me summarize the commonly used methods. ...