PHP CodeBase: Generate n non-repeating random numbers

Source: Internet
Author: User
Tags shuffle


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 certain number of distinct random numbers can be generated by storing random numbers in an array and then removing duplicate values in the array.

The procedure is as follows:

<?php
/*
* 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:

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 ().
Removing duplicate values in the array uses the "rollover" method, which uses Array_flip () to swap the array's key and value two times. 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.
























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 certain number of distinct random numbers can be generated by storing random numbers in an array and then removing duplicate values in the array.

The procedure is as follows:

01 <?php
02 /*
03 * array unique_rand( int $min, int $max, int $num )
04 * 生成一定数量的不重复随机数
05 * $min 和 $max: 指定随机数的范围
06 * $num: 指定生成数量
07 */
08 functionunique_rand($min, $max, $num) {
09     $count= 0;
10     $return= array();
11     while($count< $num) {
12         $return[] = mt_rand($min, $max);
13         $return= array_flip(array_flip($return));
14         $count= count($return);
15     }
16     shuffle($return);
17     return$return;
18 }
19
20 $arr= unique_rand(1, 25, 16);
21 sort($arr);
22
23 $result= ‘‘;
24 for($i=0; $i< count($arr);$i++)
25 {
26     $result.= $arr[$i].‘,‘;
27 }
28 $result= substr($result, 0, -1);
29 echo$result;
30 ?>

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 ().
    • Removing duplicate values in the array uses the "rollover" method, which uses Array_flip () to swap the array's key and value two times. 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.

PHP CodeBase: Generate n non-repeating random numbers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.