C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » PHP

rand() function in PHP

PHP - rand() function: In this article, we are going to learn about rand() function, how to generate random numbers between min and max?
Submitted by IncludeHelp, on January 08, 2018

Given min and max number and we have to print random number between them using PHP rand() function.

rand() function is used to generate random number in PHP, it is an in-built function and can be used to get any random number between given range.


rand(min, max);


  1. min – defines the minimum range of the random number.
  2. max – defines the maximum range of the random number.

Note: If we do not specify ranges, rand() will generate random number from 0 to getrandmax().

PHP code

    //Generating random number from 0 to genrandmax()
    $num = rand();
    print_r('Random number is: ' . $num . "\n");
    //second time
    $num = rand();
    print_r('Random number is: ' . $num . "\n");
    //Generating random number from 100 to 200
    $num = rand(100,200);
    print_r('Random number is: ' . $num . "\n");    
    //second time
    $num = rand(100,200);
    print_r('Random number is: ' . $num . "\n");        


Random number is: 1399397815
Random number is: 453651028
Random number is: 175
Random number is: 185

You may also be interested in...

C/C++ Tips and Tricks...

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions