Generate a random integer with PHP

Posted by Shek on May 10, 2008 under PHP | Be the First to Comment | Total View: 693 views

In this post, I am going to show how to generate a random integer with PHP.


To generate a random integer with PHP, there is a function called rand( ) which does the job.

Depends on whether you want the generation number is within a certain range or not, rand( ) takes in 2 parameters:

rand(minimum of the range inclusive, maximum of the range inclusive) the 2 parameters are integers. If you do not input any parameter into the function, the generation range will be between 0 and RAND_MAX inclusive.

Here are a few examples:

To have an random integer between 0 and RAND_MAX inclusive:

1
2
3
<?php
echo rand( );
?>

If you want to have a random integer between 1 and 50 inclusive, we do this:

1
2
3
<?php
echo rand(1,50);
?>

Similar Posts:

VN:F [1.9.13_1145]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.13_1145]
Rating: 0 (from 0 votes)

Add A Comment

*