PHP program to generate a random integer between the ranges

Here, we will figure out how to produce arbitrary integers between the given extents (start value and end value) in PHP?

Irregular integer number age

To produce an arbitrary integer, we can utilize random_int() function, which is a library function in PHP, it acknowledges two contentions start_value and end_value and returns an irregular integer among start_value and end_value.

Example:

<?php
    $start = 10;
    $end = 20;
    
    //generating 10 number
    print("random integers between $start and $end...\n");
    for($i=1; $i<=10; $i++)
    {
        print(random_int($start, $end)." ");
    }
    print("\n");
    
    $start = -100;
    $end = 0;    

    //generating 10 number
    print("random integers between $start and $end...\n");
    for($i=1; $i<=10; $i++)
    {
        print(random_int($start, $end)." ");
    }    
?>

Output

random integers between 10 and 20...
15 15 16 17 10 15 15 13 14 13
random integers between -100 and 0...
-84 -4 -68 -36 -18 -56 -53 -31 -98 -94

Leave a Comment

error: Alert: Content is protected!!