PHP rand

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

PHP : rand() function

Description

PHP rand() function is used to generate a random integer

Version

(PHP 4 and above)

Syntax

rand()

rand(minv, maxv)

Parameters

Name

Description

Required /
Optional

Type

minv

Lowest value to be returned.
Default : 0

Optional

Integer

maxv

Highest value to be returned.
Default : getrandmax())

Optional

Integer

Note : If the function is called without the minv and maxv arguments rand() returns a pseudo-random value between 0 and getrandmax(). If you want a random number between 15 and 25 (inclusive), for example, use mt_rand(15, 25).

Return value

A pseudo random value between minv and maxv.

Value Type : Integer.

Example :

  1. $rand1=rand();  

  2. $rand2=rand(15,25);  

  3. $rand3=rand(-150,300);  

  4. echo "Random number without a range : ". $rand1 . "
    ";  

  5. echo "Random number between 15 to 25  is " . $rand2 . "
    ";  

  6. echo "Random number between -150 to 300  is " . $rand3 . "
    ";  

  7. ?>