
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
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.No | Parameter & Description |
---|---|
1 | min lower limit of range to return a number from. Default is 0 |
2 | max 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
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
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
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
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 ';'
- Related Articles
- rand() function in PHP
- rand() and srand() in C
- PHP Function arguments
- PHP abs() Function
- PHP acos() Function
- PHP acosh() Function
- PHP asin() Function
- PHP asinh() Function
- PHP atan() Function
- PHP atan2() Function
- PHP atanh() Function
- PHP base_convert() Function
- PHP bindec() Function
- PHP ceil() Function
- PHP cos() Function
