Multiple random numbers in the range specified by php

Source: Internet
Author: User

Call mt_rand () to generate a random number. The parameter is the minimum and maximum values of the range. The function returns a random number between the minimum and maximum values.

Generating real random numbers is not easy for calculation.

In php, two methods can generate random numbers. One classic function is rand (), and the other better function is mt_rand ().


Example 1

The code is as follows: Copy code


$ Random = rand (0,1000 );

Or

<? Php
$ Rand = mt_rand (1,100 );
 
Echo $ rand;
?>

Example 2

The code is as follows: Copy code

Srand (double) microtime () * 1000000 );
$ Random = rand (0,1000 );

Example 3

The code is as follows: Copy code

/**
* Obtain multiple random numbers in a certain range.
*/
Function yang_numberRand ($ begin = 0, $ end = 20, $ limit = 5 ){
$ Rand_array = range ($ begin, $ end );
Shuffle ($ rand_array); // call the random array arrangement function.
Return array_slice ($ rand_array, 0, $ limit); // The first $ limit
}

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.