首页  >  文章  >  后端开发  >  PHP 中的随机数生成器

PHP 中的随机数生成器

王林
王林原创
2024-08-29 13:13:13701浏览

在本文中,我们将学习 PHP 中的随机数生成器。那么什么是随机数生成器?

我们可以使用内置函数生成随机数或整数。这些功能有什么作用?这些函数在最小值和最大值范围内生成不同的数字集。每次调用此函数时,它都会生成一个唯一的数字。我们可以生成任何编号的数字,例如 2 位数字、3 位数字等。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

数字在范围内进行洗牌并相应地生成。有各种内置函数可以生成随机数。

随机数生成器函数

现在我们将学习生成伪随机数的不同函数:

  • 不带范围的 rand() 函数,带范围的 rand() 函数:调用此函数时会返回一个随机数。当向函数提供最小值和最大值时,它会生成该范围内的随机数。
  • mt_rand() 函数: 该函数与 rand() 类似。 mt_rand() 中的 mt 代表 Mersenne Twister。 mt_rand() 函数是一个随机数生成器并返回一个整数值。它像 rand() 函数一样生成伪随机数。这是第一个伪随机数生成器。它是较旧的随机数生成器的高级形式。它快速、高效并提供高质量的整数。
  • getrandmax() 函数: 该函数没有定义任何参数,顾名思义,它返回最大或最大可能的随机数。
  • mt_getrandmax() 函数: 它与 getrandmax() 函数类似,它也返回最大或最大可能的随机数。这里 mt 再次代表 Mersenne Twister,它是一种生成随机数的算法。
  • srand(seed) 函数: 如果未给定此函数种子与随机数,则此函数使用给定的种子值种子随机数生成器
  • mt_srand(seed): 此函数类似于 srand() 函数,该函数使用给定的种子值来为随机数生成器播种。

我们将通过所提到的每种类型函数的示例来学习语法。

1. rand() 函数

语法:

rand()

示例:

<?php
// program to generate random integer value
echo '<br>'.'Following are the different random values';
echo '<hr/>';
echo '<br>'. rand();
echo '<hr/>';
echo '<br>'. rand();
echo '<hr/>';
echo '<br>'. rand();
?>

输出:

PHP 中的随机数生成器

2. rand() 给定范围内的函数

此函数为 rand() 函数提供范围。

语法:

rand(min, max);

其中 min 是可选的最小值,表示最低数值,max 是可选的最大值,表示最高数值。

此外,min 的默认值为零,max 的默认值是 getrandmax() 函数值。函数的返回类型始终是整数。

示例:

<?php
// program to generate random integer value
echo 'Following are the different random values within ranges min and max';
echo '<hr/>';
echo '<br> Range : 1 to 100 ----> '. rand(1,100);
echo '<hr/>';
echo '<br> Range 5 to 25 ---->'. rand(5, 25);
echo '<hr/>';
echo '<br>Range 10000 to 50000 --->'. rand(10000, 50000);
?>

输出:

PHP 中的随机数生成器

3. mt_rand() 函数

语法:

int mt_rand(min, max)

其中 min 是可选值,表示最小数字,max 是可选值,表示最大数字。 min 的默认值为 0,max 的默认值为给定的最高值。返回类型是整数。

示例:

<?php
// program to generate random integer value
echo 'Following are the different random values using mt_rand()';
echo '<hr/>';
echo '<br> Range : 1 to 100 ----> '. mt_rand(1,100);
echo '<hr/>';
echo '<br> Range 5 to 25 ---->'. mt_rand(5, 25);
echo '<hr/>';
echo '<br>Range 9 to 19 --->'. mt_rand(9, 19);
?>

输出:

PHP 中的随机数生成器

4. getrandmax() 函数

语法:

mt_getrandmax();

该函数返回一个整数值

示例

<?php
// program to generate random integer values
//using getrandmax() function
echo 'Random number using getrandmax() function';
echo '<hr/>';
echo(getrandmax());
echo '<hr>';
?>

输出:

PHP 中的随机数生成器

5. mt_getrandommax() 函数

语法:

mt_getrandmax();

此函数返回一个整数值。

示例:

<?php
// program to generate random integer values
//using mt_getrandmax() function
echo 'random number using mt_getrandmax() function';
echo '<hr/>';
echo(mt_getrandmax());
?>

输出:

PHP 中的随机数生成器

6. srand() 函数

语法:

srand(seed);

其中种子是可选值,并且此函数不返回任何内容。

示例:

<?php
// program to generate random integer value
echo 'example using srand';
echo '<br>'. srand(3);
echo(rand(1, 5));
echo '<hr>';
echo 'example using srand';
echo '<br>'. srand(2);
echo(rand(1, 5));
?>

输出:

PHP 中的随机数生成器

7. mt_srand() Function

Example:

<?php
// program to generate random integer value using mt_srand() function
echo 'example using mt_srand';
echo '<hr>';
mt_srand(5);
echo mt_rand(1,5);
?>

Output:

PHP 中的随机数生成器

Generation Integers

In the following example we have used rand(),rand(min,max) and mt_rand().

Code:

<?php
// program to generate random integer value
echo 'Following are the different random values';
echo '<br> Any random number ---->'. rand();
echo '<br> Any random number ---->'. rand();
echo '<hr>';
// random number with range
echo 'Following are the different random values within a range ';
echo '<br> Any random number within the range from 0 to 9----> '. rand(0,9);
echo '<br>Any random number within the range from 1000  to 9999 ---->'. rand(1000,9999);
echo '<hr>';
// random number with range
echo 'Following are the different random values using mt_rand() ';
echo '<br> Using mt_rand()---->'. mt_rand(1000,9999);
echo '<br> Using mt_rand()---->'. mt_rand(100,999);
?>

Output:

PHP 中的随机数生成器

Generation Floating-Point Numbers

Floating-point numbers represent a number with decimals that are of the type float. Examples – 10.0, 8.12, 6.23e-5, 2.345, 2.98e+10 and more.

Code:

<?php
function fun($min, $max) {
$square_root = sqrt(4);
return mt_rand($min * $square_root, $max * $square_root) /  100;
}
echo 'Program to display floating point numbers ';
echo '<hr>';
echo "<br>".fun(1, 10, 2);
?>

Output:

PHP 中的随机数生成器

Conclusion

In this article, we learned about various functions used to generate a random number in PHP. These functions are explained with sample examples. Hope this article is found useful to anyone who wants to learn a random number generator in PHP.

以上是PHP 中的随机数生成器的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
上一篇:PHP base64_decode下一篇:Palindrome in PHP