rand() function in PHP

rand() function in PHP

PHP – rand() function: In this article, we will find out about rand() function, how to create irregular numbers among min and max?

Given min and max number and we need to print irregular number between them utilizing the PHP rand() function.

rand() function is utilized to produce an arbitrary number in PHP, it is an in-assembled function and can be utilized to get any irregular number between a given range.

Syntax:

rand(min, at the most); 

Here,

  • min – characterizes the base scope of the arbitrary number.
  • max – characterizes the most extreme scope of the arbitrary number.

Note: If we don’t determine ranges, rand() will produce an irregular 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

Recent Posts

error: Alert: Content is protected!!