Sort out another php method to generate random numbers without repetition-PHP source code

Source: Internet
Author: User
There are many built-in Random Functions in php. These functions can generate random but may be repeated multiple times. The following lists some functions that generate random numbers without repeated numbers. There are many built-in Random Functions in php. These functions can generate random but may be repeated multiple times. The following lists some functions that generate random numbers without repeated numbers.

Script ec (2); script

Random numbers are useful for Web applications, WAP or mobile applications. In recent small projects, I often need to deal with random numbers or random arrays. Therefore, I would like to summarize several common methods for generating non-repeated random numbers in PHP (ps: methods 1, 4, and 5 are commonly used, and the rest are from network arrangement)

Method 1:

The Code is as follows:
$ Numbers = range (1, 50 );
// Shuffle will immediately disrupt the array order
Shuffle ($ numbers );
// Array_slice retrieves a segment of the array
$ Num = 6;
$ Result = array_slice ($ numbers, 0, $ num );
Print_r ($ result );
?>

Method 2:

The Code is as follows:
$ Numbers = range (1, 20 );
// Broadcast the seed of the random number generator, which is dispensable and does not affect the test result.
Srand (float) microtime () * 1000000 );
Shuffle ($ numbers );
// Skip the first value of the list (the index is saved)
While (list (, $ number) = each ($ numbers )){
Echo "$ number ";
}
?>

Method 3:

The Code is as follows:
Function NoRand ($ begin = 0, $ end = 20, $ limit = 5 ){
$ Rand_array = range ($ begin, $ end );
Shuffle ($ rand_array); // call the random array arrangement function.
Return array_slice ($ rand_array, 0, $ limit); // the first $ limit
}
Print_r (NoRand ());
?>

Five distinct values can be randomly generated between 1 and 20.

Method 4:

The Code is as follows:
$ Tmp = array ();
While (count ($ tmp) <5 ){
$ Tmp [] = mt_rand (1, 20 );
$ Tmp = array_unique ($ tmp );
}
Print_r ($ tmp );
?>

Method 5:

The Code is as follows:

$ Tmp = range (1, 30 );
Print_r (array_rand ($ tmp, 10 ));
?>

This may be simpler (ps: If the step is specified in the range, you must note whether the second parameter of array_rand exceeds the length of $ tmp ).

PHP provides a rich array function, and generates random numbers most of the time from the array perspective. If you have other methods available, please refer to the article for continuous updates.

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.