Analysis of methods for generating non-repeated random numbers using php

Source: Internet
Author: User
Analysis of methods for generating non-repeated random numbers using php

  1. Function get_randoms ($ min, $ max, $ num ){
  2. $ Count = 0;
  3. $ Res = array ();
  4. While ($ count <$ num ){
  5. $ Res [] = mt_random ($ min, $ max );
  6. $ Res = array_flip ($ res ));
  7. $ Count = count ($ res );
  8. }
  9. Return $ res;
  10. }
  11. $ Result = get_randoms (1, 60, 10 );
  12. ?>

Here he uses the number of $ count result arrays. in the while loop, the number of result arrays is smaller than the number of targets. In this way, $ count is an array of the number of targets. (Bbs.it-home.org script School) during each cycle, through mt_random ($ min, $ max), a random value in the interval, stored in the array $ res, then, the key-value is exchanged twice, that is, the uniqueness of the array key makes the key unique.

After reading the logic code, I can't help but think deeply. if you don't know the array_filp function, what do you do? To put it bluntly, the uniqueness of the key is used so that the random number will not be repeated. So can we simplify this problem?

Code:

  1. Function get_randoms ($ min, $ max, $ num ){
  2. $ Count = 0;
  3. $ Res = array ();
  4. While ($ count <$ num ){
  5. $ Key = mt_random ($ min, $ max );
  6. $ Res [$ key] = $ key;
  7. $ Count = count ($ res );
  8. }
  9. Return $ res;
  10. }
  11. $ Result = get_randoms (1, 60, 10 );
  12. ?>

In this way, the uniqueness of the array key solves this problem. Array_flip is not used at all.

Conclusion: 1. because php provides a large number of built-in functions, their execution efficiency is very high. (Bbs.it-home.org programmer's house) when encountering problems, try to use it to solve the problem. This is convenient and fast.

2. php provides a large number of built-in functions with high execution efficiency. However, although the efficiency is high, it still takes time to execute them. When you encounter a problem, do not blindly do it. First, you can see the essence of the solution to facilitate the clever implementation of it.

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.