PHPSuperBlog.com

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:

<?php
echo rand( );
?>

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

<?php
echo rand(1,50);
?>
VN:F [1.9.3_1094]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.3_1094]
Rating: 0 (from 0 votes)

Similar Posts:

Leave a comment