How to generate a PHP Random Number?

How to generate a PHP Random Number? This is simple tutorial to generate random number in php using PHP rand function. This PHP rand function can be used to generate a random number from a minimum and maximum number limit or we can generate a complete random number. Here is the syntax of PHP rand function and example code for using PHP rand function, and generating random number from PHP rand function.

Syntax of php rand function

Generate a random number without setting any limits.


If you want to set a upper & lower limit for generating a random number then use $min, $max variables. Where $min is the minimum number and $max is the maximum upper limit for generating random number.

rand($min, $max)

You can use minimum and maximum values for generating a php random number between two values one is lower value and the other one is upper value.

For example if you want to generate a number between 100 to 1000, then we can php rand function to do this by using below code.
It generates a number in between 100 and 1000, but not less than 100 and not greater than 1000.

rand(100, 1000)

In the same way if you want to generate a five digit number, then use this below code.

Then random number will be in between 10000 and 99999, so that it is always generates a five digit number.

rand(10000, 99999)

Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: