A set of non-repeated number implementation programs are randomly generated in PHP.

Source: Internet
Author: User
The following describes how to randomly generate a set of non-repeated number implementation code in two PHP programs. For more information, see. The code is as follows: Copy the code? Php **...

The following describes how to randomly generate a set of non-repeated number implementation code in two PHP programs. For more information, see.

The code is as follows:

/**
* PHP obtains a set of random numbers without duplicates.
*/
$ A = microtime ();
Function createRandID ($ m ){
// Generate an array from 1 to $ m
$ Arr = range (1, $ m );
// Disrupt the array
Shuffle ($ arr );
// Obtain the first 10
For ($ I = 0; $ I <= 10; $ I ++ ){
// Assign a value to the new array $ n
$ N [] = $ arr [$ I];
}
// Return this set of numbers
Return implode ($ n ,',');
}

Echo createRandID (700000 );
Echo'
';
Echo $ a-microtime ();
?>

Execution result:

560875,593409, 325987,658308, 248054,205426, 375413,676243, 485853,575393, 115975
0.672761

The above results show that the time spent is 0.6. We can adjust the random number range from 700000 to 900000 and then check the execution result.

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 7200000 bytes) in/data0/htdocs/www/a. php on line 10

The array is too large to run !!

The code is as follows:

/**
* PHP obtains a set of random numbers without duplicates.
* Qiongtai blog
*/
$ A = microtime ();
Function createRandID ($ m ){
// Note: you must declare an empty array first. Otherwise, the in_array in while will report an error.
$ Arr = array ();
// Use the while loop. if there are less than 10, the loop will always be done.
While (count ($ arr) <= 10 ){
// Generate a random number
$ A = rand (1, $ m );
// Judgment: If the generated random number is no longer in the array, it is assigned to the array.
// Avoid repeated numbers
If (! In_array ($ a, $ arr )){
// Assign a random number to an array
$ Arr [] = $;
}
}
// Return the generated random number
Return implode ($ arr ,',');
}
Echo createRandID (700000 );
Echo'
';
Echo $ a-microtime ();
?>

Execution result:

308326,155128, 280369,493174, 214855,219990, 482837,66329, 512934,232527, 386975
0.00015699999999996

We can see from the preceding execution results that the time is negligible. we can adjust the random number range from 700000 to 999999 to see the execution result.

392281,822956, 401282,176255, 143076,501802, 393338,546922, 21836,601991, 362006
0.00013600000000002

The execution result has nothing to do with the maximum value setting. it is still running fast!



Article URL:

Reprint ^ at will, but please attach the tutorial address.

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.