PHP generates N non-repeated random numbers. php generates n-random number _ PHP Tutorial

Source: Internet
Author: User
PHP generates N non-repeated random numbers. php generates n random numbers. PHP generates N non-repeated random numbers, and php generates n random numbers. cause: If 25 pieces are used for voting, 16 are required for one vote, and only one vote for a single piece is allowed. The preceding PHP generates N non-repeated random numbers, and php generates n random numbers.


You can vote for 25 pieces of data. you can only vote for 16 pieces of data at a time. The previous programmer threw a loophole and forgot to put the voting into the database. the voting sequence generated by 200 users was empty. So how do you fill in this leak?

Of course, reflect the situation to the superior. However, we will discuss the technology here, that is, we need to generate 16 non-repeated random numbers between 1 and 25 to fill. How to design functions? Store a random number into an array, and then remove duplicate values in the array to generate a certain number of non-repeated random numbers.

The procedure is as follows:

The code is as follows:

<? Php
* Array unique_rand (int $ min, int $ max, int $ num)
* Generate a certain number of non-repeated random numbers
* $ Min and $ max: specify the random number range.
* $ Num: specifies the number of instances generated.
Function unique_rand ($ min, $ max, $ num ){
$ Count = 0;
$ Return = array ();
While ($ count <$ num ){
$ Return [] = mt_rand ($ min, $ max );
$ Return = 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:

The code is as follows:

Additional notes:

The mt_rand () function is used to generate a random number. The average speed of this function to generate random numbers is four times faster than rand.
When the repeated values in the array are removed, the "flip method" is used to exchange the key and value of the array twice 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 array to ensure that the key name is a continuous number ranging from 0 to n. If you do not perform this step, the key name may not be consecutive when you delete duplicate values, which may cause trouble for traversal.

Http:// to generate N non-repeated random number, php to generate n random number cause: 25 works to vote, a vote needs to select 16, a single work only one vote. There is...

Related Article

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: 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.