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

Source: Internet
Author: User
Randomly generated numbers are often applied to random passwords or random verification codes. PHP has the rand () interference function and MD5 function. let's take a look at the combination of PHP functions to generate a set of unique

Randomly generated numbers are often applied to random passwords or random verification codes. PHP uses the rand () interference function and MD5 to implement this, next, let's take a look at how to use a program to generate a set of non-repeated numeric arrays using PHP functions, and directly use the code:

  1.  
  2. /**
  3. * PHP obtains a set of random numbers without duplicates.
  4. */
  5. $ A = microtime ();
  6. Function createRandID ($ m ){
  7. // Generate an array from 1 to $ m
  8. $ Arr = range (1, $ m );
  9. // Disrupt the array
  10. Shuffle ($ arr );
  11. // Obtain the first 10
  12. For ($ I = 0; $ I <= 10; $ I ++ ){
  13. // Assign a value to the new array $ n
  14. $ N [] = $ arr [$ I];
  15. }
  16. // Return this set of numbers
  17. Return implode ($ n ,',');
  18. }
  19. Echo createRandID (700000 );
  20. Echo' ';
  21. Echo $ a-microtime ();
  22. ?>

Execution result:

560875,593409, 325987,658308, 248054,205426, 375413,676243, 485853,575393, 1159750.672761

From the above results, we can see that it took 0.6 seconds to adjust the random number range from 700000 to 900000. then we can see the execution result: Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 7200000 bytes) in/data0/htdocs/www/. php on line 10

The code is as follows:

  1. /**
  2. * PHP obtains a set of random numbers without duplicates.
  3. * PHP blog
  4. */
  5. $ A = microtime ();
  6. Function createRandID ($ m ){
  7. // Note: you must declare an empty array first. Otherwise, the in_array in while will report an error.
  8. $ Arr = array ();
  9. // Use the while loop. if there are less than 10, the loop will always be done.
  10. While (count ($ arr) <= 10 ){
  11. // Generate a random number
  12. $ A = rand (1, $ m );
  13. // Judgment: If the generated random number is no longer in the array, it is assigned to the array.
  14. // Avoid repeated numbers
  15. If (! In_array ($ a, $ arr )){
  16. // Assign a random number to an array
  17. $ Arr [] = $;
  18. }
  19. }
  20. // Return the generated random number
  21. Return implode ($ arr ,',');
  22. }
  23. Echo createRandID (700000 );
  24. Echo' ';
  25. Echo $ a-microtime ();
  26. ?>

Execution result:

308326,155128, 280369,493174, 214855,219990, 482837,66329, 512934,232527, 3869750.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, 3620060.00013600000000002

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

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.