Tutorialspoint.dev

PHP | rand() function


The rand() is an inbuilt-function in PHP used to generate a random number.

Syntax:

rand() 
The rand() function is use to generate a random integer.

To generate a random integer in some range:
Syntax

rand(min,max)
min specifies the lowest value that will be returned.
max specifies the highest value to be returned.

This function will generate a random value in the range [min,max]

Note
If the min and max value is not specified, default is 0 and getrandmax() respectively.

Example

  1. rand() will return a random integer between 0 and getrandmax().
  2. rand(15,35) will return a random integer in the range [15,35].
<?php
      
    // Generating a random number
    $randomNumber = rand();
      
    // Print
    print_r($randomNumber);
  
    // New Line
    print_r(" ");
      
    // Generating a random number in a 
    // Specified range.
    $randomNumber = rand(15,35);
      
    // Print
    print_r($randomNumber);
      
?>

Note :
The output of the code may change every time it is run. So the output may not match with the specified output.
Output:

1257424548
28


This article is attributed to GeeksforGeeks.org

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter