PHP | gmp_random_range() Function

The gmp_random_range() is an inbuilt function in PHP which generates a random number.The random number thus generated lies between range min to max. Here GMP refers to (GNU Multiple Precision) which is for large numbers.

Syntax:

gmp_random_range ( GMP $min, GMP $max )

Parameters: The function accepts two parameters, GMP $min number representing lower bound for the random number and GMP $max number to represent the upper bound of the random number. This parameter can be a GMP object in PHP version 5.6 and later, or we are also allowed to pass a numeric string provided that it is possible to convert that string to a number.



Return Value: The function returns a random GMP number in the range $min-$max.

Examples:

Input : lower bound=0, upper bound =100
Output :  25

Input : lower bound=-100, upper bound=-10
Output :  -23 

Note:Output will vary every time on execution

Below programs illustrate the use of gmp_random_range() function:

Program 1: The program below demonstrates the working of gmp_random_range() function when numeric strings are passed as arguments.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the gmp_random_range() function 
  
// numeric string as arguments
$min = "-200";
$max = "-100";
  
  
$rand = gmp_random_range($min, $max); 
  
echo $rand;
?>

chevron_right


Output:

-165

Program 2: The program below demonstrates the working of gmp_random_range() when GMP number is passed as an argument.

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to demonstrate the gmp_random_range() function 
  
// GMP numbers as arguments
$min = gmp_init("1000", 2);
$max = gmp_init("1000000", 2); 
  
  
$rand = gmp_random_range($min, $max); 
  
// gmp_strval converts GMP number to string 
// representation in given base(default 10).
echo gmp_strval($rand) . "\n";
?>

chevron_right


Output:

30

Reference:
http://php.net/manual/en/function.gmp-random-range.php



My Personal Notes arrow_drop_up

Contented with little yet wishing for more

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.