PHP uses codebase to generate random numbers

Source: Internet
Author: User
Tags min shuffle

  This article mainly introduces PHP using codebase to generate random numbers of examples, the need for friends can refer to the following

There are 25 pieces to vote on, one vote needs to choose 16, and one vote for a single work only once. A programmer in front of the mess, forgot to put the vote into the library, there are 200 users of the voting sequence is empty. So how are you going to fill this mess?   Of course to the superior to reflect the situation. But the technique we're talking about here is the need to generate 1-25 of the 16 random numbers that are not duplicated to fill. Specifically how to design the function? By storing the random number in an array and then removing the duplicate value in the array, a certain number of distinct random numbers can be generated. The   procedure is as follows:     code is as follows: <?php/* array unique_rand (int $min, int $max, int $num) * Generates a certain number of distinct random numbers * $min and $ Max: Specify a range of random numbers * $num: Specify build quantity/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?>       program runs as follows:     code 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 to generate random numbers. This function generates an average speed of random numbers four times times faster than Rand (). Remove duplicate values in the array using the "rollover method", that is, using Array_flip () to exchange the key and value of the array two times. This approach is much faster than using Array_unique (). Before returning the array, the shuffle () is used to assign the new key name to the number of 0-n consecutive digits. If you do not do this, you may have trouble with traversal by causing the key name to be discontinuous when you delete duplicate values.  

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.