PHP function: Generate N non-repeated random numbers and php random numbers.

Source: Internet
Author: User

PHP function: Generate N non-repeated random numbers and php random numbers.
PHP function: Generate N random numbers that are not repeated

Idea: store the generated random number into an array, and then remove duplicate values in the array to generate a certain number of non-repeated random numbers.

 

Program:

<? Php/** array unique_rand (int $ min, int $ max, int $ num) * generate a certain number of random numbers not repeated * $ min and $ max: specify the random number range * $ num: specify the number of generated records */function unique_rand ($ min, $ max, $ num) {$ count = 0; $ return_arr = array (); while ($ count <$ num) {$ return_arr [] = mt_rand ($ min, $ max); $ return_arr = array_flip ($ return_arr )); $ count = count ($ return_arr);} shuffle ($ return_arr); return $ return_arr ;}

Note:

1. The mt_rand () function is used to generate random numbers. This function is four times faster than the rand () function;

2. When the array repeated values are removed, the "flip method" is used, that is, the array key and value are exchanged twice with array_flip. It is much faster than array_unique.


How does matlab generate random numbers that are not repeated? Rand (1, 8) * 100
Ans =
Columns 1 through 7
81.4724 90.5792 12.6987 91.3376 63.2359 9.7540 27.8498
Column 8
54.6882
Use excel to generate N random numbers. How can I use the if function so that A1: A1000 is not selected repeatedly? In the editing column, enter
= RAND ()
Press CTRL + press Enter]
Select B1: B1000, and enter
= RANK (A1, a $1: A $1000)
Press CTRL + press Enter]

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.