Home  >  Article  >  Backend Development  >  mt_rand is a function that PHP has had since its inception

mt_rand is a function that PHP has had since its inception

藏色散人
藏色散人Original
2021-12-14 10:25:391129browse

mt_rand is a function available since php4.2.0. This function is used to generate better random numbers. The syntax is "mt_rand(): int" or "mt_rand(int $min, int $max): int".

mt_rand is a function that PHP has had since its inception

#The operating environment of this article: Windows 7 system, PHP version 7.4, Dell G3 computer.

mt_rand is a function that PHP has since its inception?

Since PHP 4.2.0, it is no longer necessary to seed the random number generator with the srand() or mt_srand() functions, it is now done automatically. In versions prior to 3.0.7, max meant range . To get the same random numbers from 5 to 15 as in the above example in these versions, the short example is mt_rand (5, 11).

mt_rand

(PHP 4, PHP 5, PHP 7, PHP 8)

mt_rand — Generate better random numbers

Description

mt_rand(): int
mt_rand(int $min, int $max): int

Many old libc random number generators have some uncertain and unknown characteristics and are very slow. PHP's rand() function uses the libc random number generator by default. The mt_rand() function is informally used to replace it. This function uses the known features of » Mersenne Twister as a random number generator, which can generate random values ​​on average four times faster than rand() provided by libc.

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

Parameters

min

Optional, the minimum value returned (default: 0)

max

Optional, The maximum value returned (default: mt_getrandmax())

Return value

Return a random integer between min (or 0) and max (or to mt_getrandmax(), including this value) .

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of mt_rand is a function that PHP has had since its inception. For more information, please follow other related articles on the PHP Chinese website!

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