MENU
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
2147483647
984577095
-34
0.69584758753416
| 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) |
<!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)
7151a40f1c74356f
31
bool(true)
<!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
72
7
76