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.

Syntax:

rand(min, max);

Here,

  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

<?php
    //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");        
?>

Output

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





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.




Comments and Discussions





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.