w3resource

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:

<?php
$rand1=rand();
$rand2=rand(15,25);
$rand3=rand(-150,300);
echo "Random number without a range : ". $rand1 . "<br />";
echo "Random number between 15 to 25  is " . $rand2 . "<br />";
echo "Random number between -150 to 300  is " . $rand3 . "<br />";
?>

View the example in the browser

See also

PHP Function Reference

Previous: rad2deg
Next: round



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/php/function-reference/rand.php