PHPn non-repeated random number generation code _ PHP Tutorial

Source: Internet
Author: User
Code for generating non-repeated PHPn random numbers. Copy the code as follows :? Phprange is an array consisting of 1 to 100 columns $ numbersrange (1,100); shuffle disconnects the array order immediately shuffle ($ numbers); array_slice obtains the array The code is as follows:


// Range is an array of columns from 1 to 100.
$ Numbers = range (1, 1,100 );
// Shuffle will immediately disrupt the array order
Shuffle ($ numbers );
// Array_slice retrieves 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 );
?>


The code is as follows:


// Range is an array of columns from 1 to 42.
$ Numbers = range (1, 42 );
// Shuffle will immediately disrupt the array order
Shuffle ($ numbers );
// Array_slice retrieves a segment of the array
$ Result = array_slice ($ numbers, 0, 3 );
Print_r ($ result );


Method 2

The code is as follows:


$ Numbers = range (1, 20 );
Srand (float) microtime () * 1000000 );
Shuffle ($ numbers );
While (list (, $ number) = each ($ numbers )){
Echo "$ number ";
}
?>

Method 3
With PHP, 5 non-repeated values are randomly generated between 1 and 20. how can this problem be solved?

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 ());
?>


Or do not shuffle

The code is as follows:


$ Tmp = array ();
While (count ($ tmp) <5 ){
$ Tmp [] = mt_rand (1, 20 );
$ Tmp = array_unique ($ tmp );
}
Print join (',', $ tmp );
?>

The http://www.bkjia.com/PHPjc/320353.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/320353.htmlTechArticle code is as follows :? Php // range is an array of columns from 1 to 100. $ numbers = range (1,100); // shuffle disconnects the array order and shuffle ($ numbers ); // array_slice fetch the array...

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.