Generate a certain number of non-repeating random number of PHP code

Source: Internet
Author: User
Generate a certain number of non-repeating random numbers
    1. /*
    2. * Array Unique_rand (int $min, int $max, int $num)
    3. * Generate a certain number of non-repeating random numbers
    4. * $min and $max: Specify the range of random numbers
    5. * $num: Specify the number of builds
    6. */
    7. function Unique_rand ($min, $max, $num) {
    8. $count = 0;
    9. $return = Array ();
    10. while ($count < $num) {
    11. $return [] = Mt_rand ($min, $max);
    12. $return = Array_flip (Array_flip ($return));
    13. $count = count ($return);
    14. }
    15. Shuffle ($return);
    16. return $return;
    17. }
    18. $arr = Unique_rand (1, 25, 16);
    19. Sort ($arr);
    20. $result = ";
    21. for ($i =0; $i < count ($arr); $i + +)
    22. {
    23. $result. = $arr [$i]. ', ';
    24. }
    25. $result = substr ($result, 0,-1);
    26. echo $result;
    27. ?>
Copy Code
Php
  • 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.