PHP | gmp_range()函数

这个 gmp_随机_范围() 是PHP中的一个内置函数,它生成一个随机数。由此产生的随机数介于最小值到最大值之间。这里指的是GMP( 多精度 )这是针对大数字的。

null

语法:

gmp_random_range ( GMP $min, GMP $max )

参数: 该函数接受两个参数:GMP $min 表示随机数和GMP的下界的数字 $max 表示随机数上限的数字。该参数可以是PHP 5.6版及更高版本中的GMP对象,也可以传递数字字符串,前提是可以将该字符串转换为数字。

返回值: 该函数返回一个范围为$min-$max的随机数。

例如:

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

下面的程序说明了 gmp_随机_范围() 功能:

项目1: 下面的程序演示了当数字字符串作为参数传递时,gmp_random_range()函数的工作原理。

<?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 ;
?>


输出:

-165

项目2: 下面的程序演示了当gmp编号作为参数传递时,gmp_random_range()的工作原理。

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


输出:

30

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

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享