Home  >  Article  >  Backend Development  >  Introduction to php rand() random number generation method_PHP tutorial

Introduction to php rand() random number generation method_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:13:50905browse

There are many ways to generate random numbers in PHP, rand() is just one of them, and the mt_rand() function can also generate them. I won’t go into the differences, let’s take a look.

rand() function returns a random integer.

Grammar
rand(min,max) parameter description
min,max are optional. Specifies the range of random number generation.

If the optional parameters min and max are not provided, rand() returns a pseudo-random integer between 0 and RAND_MAX. For example, if you want a random number between 5 and 15 (inclusive), use rand(5, 15).

In PHP, to generate a random number, you can go through the following three steps:

The code is as follows
 代码如下 复制代码
//第一步:初始化种子
$seedarray =microtime();
$seedstr =split(" ",$seedarray,5);
$seed =$seedstr[0]*1000000;
Copy code


//The first step: initialize the seed $seedarray =microtime();
 代码如下 复制代码
srand($seed);
$seedstr =split(" ",$seedarray,5);

$seed =$seedstr[0]*1000000;

 代码如下 复制代码
$random =rand(0,1000);

//Step 2: Initialize the random number generator using the seed

There is also a more simplified code for generating random numbers:
The code is as follows
 代码如下 复制代码

srand((double)microtime()*1000000);
$random =rand(0,1000);

Copy code

srand($seed);


//Step 3: Generate random numbers within the specified range

Among them, the first two steps do not need to be modified, which is the last step. (0, 1000) means generating a random number from 0 to 1000 (including 0). This range can be modified to the range you need. .
The code is as follows Copy code

$random =rand(0,1000); Among them, microtime() returns two values: the current number of milliseconds and the timestamp. Since we are just extracting random numbers, just use milliseconds. (double)microtime() means only returning the current millisecond value. microtime() is the number of milliseconds in seconds, so the values ​​are all decimals. Multiply by 1000000 to convert them to integers. The second sentence of code indicates generating a random number from 0 to 1000. Tips and Notes Note: On some platforms (e.g. Windows) RAND_MAX is only 32768. If you need a range greater than 32768, specify the min and max parameters to generate a number greater than RAND_MAX, or consider using mt_rand() instead. Note: As of PHP 4.2.0, seeding the random number generator with the srand() or mt_srand() functions is no longer necessary, it is now done automatically.
Note: In versions before 3.0.7, max means range. To get the same random numbers from 5 to 15 as in the above example in these versions, the short example is rand (5, 11).
http://www.bkjia.com/PHPjc/629086.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/629086.htmlTechArticleThere are many ways to generate random numbers in php, rand() is just one of them, and there is also the mt_rand() function. It can be generated. I won’t go into details about the difference. Let’s take a look below. The rand() function returns a random integer. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn