# PHP rand() Function

## Definition and Usage

The rand() function returns an integer using pseudo random genaration technique. default range is between 0 and platform specific getrandmax(). On 64 bit Windows OS, it is 2147483647. The rand() function can be called without arguments (in which case the default range will be used) or by specifying min and max parameters.

This function always returns an integer.

## Syntax

rand ( void ) : int
rand ( int $min , int$max ) : int

## Parameters

Sr.NoParameter & Description
1min
lower limit of range to return a number from. Default is 0
2max
Upper limit of range to return a number from. Default is getrandmax()

## Return Values

PHP rand() function returns an integer between min and max using pseudo random generation technique. Note that this is not advised to be used for cryptographic purpose.

## PHP Version

This function is available in PHP versions 4.x, PHP 5.x as well as PHP 7.x.

## Example

Live Demo

This example returns random number by calling rand() without arguments −

<?php
echo "random number with no parameters rand() = " . rand() . "";
echo "another random number with no parameters rand() = " . rand() . "";
?>

## Output

This may produce following result (it being a random number, it is more likely to return different number every time)−

random number with no parameters rand() = 1663374457
another random number with no parameters rand() = 888196648

## Example

Live Demo

Following example specifies min and max arguments for rand() function −

<?php
echo "rand(11,30) = " . rand(11,30) . "";
echo "rand(11,30) = " . rand(11,30) . "";
?>

## Output

This will produce following result −

rand(11,30) = 29
rand(11,30) = 22

## Example

Live Demo

Fractional part of float values of min and max parameters will be ignored −

<?php
echo "rand(10.5,50.95) = " . rand(10.55, 50.95) . "";
?>

## Output

This will produce following result −

rand(10.5,50.95) = 45

## Example

Live Demo

String value of min and/or max parameter will result in error

<?php
echo "rand("aa", "bb") = " . rand("aa","bb") . "";;
?>

## Output

This will produce following result −

PHP Parse error: syntax error, unexpected 'aa' (T_STRING), expecting ',' or ';'