Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

rand

Returns a random integer.

Syntax

rand(int $min = 0, int $max = getrandmax()): int

Parameters

ParameterTypeDescription
$minintOptional. The lowest value.
$maxintOptional. The highest value.

Return value

intA random number within the bounds, inclusive.

Examples

A dice roll:

<?php
 
echo rand(1, 6);

Notes

The numbers are predictable and unsuitable for passwords, tokens or prize draws. Use random_int() for those.

See also

  • mt_rand — Returns a random integer.
  • random_int — Returns a random integer from a range.
  • array_rand — Returns a random key of an array.

In the tutorial

  • Numbers and maths — PHP's maths functions — rounding, number formatting, random numbers and the precision of fractions.

Thanks for reading!

Donate to the site — we work hard for you.