Randomization

rand([$i1,$i2]) generates a random integer between 0 and getrandmax(), or between $i1 and $i2 if they are supplied. srand($i) seeds the random number generator for rand(). mt_rand ([$i1,$i2]) generates a random integer between 0 and mt_getrandmax(), or between $i1 and $i2 if they are supplied. Using the Mersenne Twister, mt_rand() is four times faster than rand(). mt_srand([$i]) seeds the random number generator for mt_rand(). lcg_value() returns a random float between 0 and 1, using the combined linear congruential generator.

<!DOCTYPE html><html><head></head>
<body><?php

echo getrandmax()."<br />";
echo mt_getrandmax()."<br />";
echo rand()."<br />";  // already seeded randomly
echo mt_rand(-100,2)."<br />"; // already seeded randomly
echo lcg_value()."<br />";

?></body></html>

2147483647
2147483647
984577095
-34
0.69584758753416
PHP math constants
Constant Description
M_PI P
M_E e
M_LOG2E log2e
M_LOG10E log10e
M_LN2 loge2
M_LN10 loge10
M_PI_2 P/2
M_PI_4 P/4
M_1_PI 1/P
M_2_PI 2/P
M_SQRTPI sqrt(P)
M_2_SQRTPI 2/sqrt(P)
M_SQRT2 sqrt(2)
M_SQRT3 sqrt(3)
M_SQRT1_2 1/sqrt(2)
M_LNPI logeP
M_EULER Euler constant
NAN Not A Number (float)
INF The Infinite (float)
PHP 7.0 added two functions for cryptographically secure randomness. random_bytes($i) returns a string of $i cryptographically secure random bytes, and random_int($i1,$i2) returns a cryptographically secure random integer between $i1 and $i2 inclusive. Unlike rand() and mt_rand(), which are fast but predictable enough to be unsuitable for anything security-sensitive, random_bytes() and random_int() are suitable for generating things like session tokens, password reset codes, and encryption keys.

<!DOCTYPE html><html><head></head>
<body><?php

$bytes = random_bytes(8);
echo strlen($bytes) . "<br />";
echo bin2hex($bytes) . "<br />";

$n = random_int(1, 100);
echo $n . "<br />";
var_dump($n >= 1 && $n <= 100);

?></body></html>

8
7151a40f1c74356f
31
bool(true)
PHP 8.2 introduced the Random extension, which separates the random number algorithm (an ‘engine’, such as Random\Engine\Mt19937 or the default cryptographically secure Random\Engine\Secure) from the Random\Randomizer class that uses it to produce values, via methods like getInt(), getBytes(), and shuffleArray(). Passing a fixed seed to an engine, such as new Random\Engine\Mt19937(1234), produces the exact same sequence of ‘random’ numbers every time. This is useful for reproducible tests or simulations, though a seeded sequence like this must never be used for anything security-sensitive.

<!DOCTYPE html><html><head></head>
<body><?php

$randomizer = new Random\Randomizer(new Random\Engine\Mt19937(1234));
echo $randomizer->getInt(1, 100) . "<br />";
echo $randomizer->getInt(1, 100) . "<br />";
echo $randomizer->getInt(1, 100) . "<br />";

// re-seeding with the same seed reproduces the same sequence
$again = new Random\Randomizer(new Random\Engine\Mt19937(1234));
echo $again->getInt(1, 100) . "<br />"; // matches the very first value above

?></body></html>

76
72
7
76