Tsallaka zuwa abun ciki
Justice Tech School

PHP Math and Random Numbers

Functions: abs, ceil, floor, round, sqrt, pow, fmod, max, min, pi, rand, mt_rand and random_int. Use random_int for secure random numbers.

Example
echo round(3.14159, 3), " ", fmod(10, 3), " ", pi() > 3, "\n";
echo intdiv(17, 5), " ", 17 % 5, " ", 2 ** 8, "\n";
$n = random_int(1, 100);
var_dump($n >= 1 && $n <= 100);
mt_srand(42); echo mt_rand(1, 10) === mt_rand(1, 10) ? "same" : "different", "\n";

Result

3.142 1 1
3 2 256
bool(true)
different

Exercise

Secure random integer.

(1, 6)

Quiz Test

Which random function is cryptographically secure?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.