mt_rand
Returns a random integer.
Syntax
mt_rand(int $min, int $max): intParameters
| Parameter | Type | Description |
|---|---|---|
| $min | int | The lower bound, inclusive. |
| $max | int | The upper bound, inclusive. |
Return value
intA random number within the bounds.
Examples
A dice roll — a different result on every run:
<?php
echo mt_rand(1, 6);The number always falls within the bounds:
<?php
$value = mt_rand(1, 6);
var_dump($value >= 1 && $value <= 6);Output:
bool(true)Notes
The numbers from mt_rand() are predictable. Use random_int() for passwords, tokens and confirmation codes.
See also
- random_int — Returns a random integer from a range.
- shuffle — Shuffles the elements of an array into a random order.
In the tutorial
- Numbers and maths — PHP's maths functions — rounding, number formatting, random numbers and the precision of fractions.