略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: mt_getrandmax

2024-05-01

mt_getrandmax

(PHP 4, PHP 5, PHP 7, PHP 8)

mt_getrandmax显示随机数的最大可能值

说明

mt_getrandmax(): int

返回调用 mt_rand() 所能返回的最大的随机数。

返回值

返回调用 mt_rand() 所能返回的最大的随机数。

范例

示例 #1 计算一个随机浮点数

<?php
function randomFloat($min 0$max 1) {
    return 
$min mt_rand() / mt_getrandmax() * ($max $min);
}

var_dump(randomFloat());
var_dump(randomFloat(220));
?>

以上例程的输出类似于:

float(0.91601131712832)
float(16.511210331931)

参见

add a noteadd a note

User Contributed Notes 1 note

up
23
bishop at php dot net
6 years ago
The upper-bound of this value is platform-independent.  PHP implements the 32-bit version of the Mersenne Twister ("mt"), so the maximum possible value is 2**31 - 1 (2147483647).

官方地址:https://www.php.net/manual/en/function.mt-getrandmax.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3