定義和用法
函數名稱中的 'mt' 前綴代表Mersenne Twister。這mt_rand()函數使用 Mersenne Twister 隨機數生成器方法返回一個整數。該函數是 PHP 的 rand() 函數的 drop-in 替代品。默認範圍介於 0 和平台特定的 mt_getrandmax() 之間。在 64 位 Windows 操作係統上,它是 2147483647。可以不帶參數調用 mt_rand() 函數(在這種情況下將使用默認範圍)或通過指定 min 和 max 參數。
這個函數總是返回一個整數。
用法
mt_rand ( void ):int mt_rand ( int $min , int $max ):int
參數
Sr.No | 參數及說明 |
---|---|
1 | min 返回數字的範圍下限。默認為 0 |
2 | max 返回數字的範圍上限。默認為 mt_getrandmax() |
返回值
PHP mt_rand() 函數使用 Mersenne Twister 隨機數生成器技術返回 min 和 max 之間的整數 該函數比 rand() 函數快四倍。請注意,不建議將其用於加密目的。
PHP版本
該函數在 PHP 4.x、PHP 5.x 和 PHP 7.x 版本中可用。
示例
此示例通過調用不帶參數的 mt_rand() 返回隨機數 -
<?php
echo "random number with no parameters mt_rand() = " . mt_rand() . "\n";
echo "another random number with no parameters mt_rand() = " . mt_rand() . "\n";
?>
輸出
這可能會產生以下結果(它是一個隨機數,更有可能每次返回不同的數字)-
random number with no parameters mt_rand() = 173620951 another random number with no parameters mt_rand() = 749065865
示例
以下示例指定 mt_rand() 函數的最小和最大參數 -
<?php
echo "mt_rand(11,30) = " . mt_rand(11,30) . "\n";
echo "mt_rand(11,30) = " . mt_rand(11,30) . "\n";
?>
輸出
這可能會產生以下結果(它是一個隨機數,更有可能每次返回不同的數字)-
mt_rand(11,30) = 24 mt_rand(11,30) = 14
示例
min 和 max 參數的浮點值的小數部分將被忽略 -
<?php
echo "mt_rand(10.5,50.95) = " . mt_rand(10.55, 50.95) . "\n";
?>
輸出
這可能會產生以下結果 -
mt_rand(10.5,50.95) = 31
相關用法
- PHP mt_rand()用法及代碼示例
- PHP mt_rand( )用法及代碼示例
- PHP mt_getrandmax()用法及代碼示例
- PHP metaphone()用法及代碼示例
- PHP mhash_get_hash_name()用法及代碼示例
- PHP mysqli_get_server_info()用法及代碼示例
- PHP money_format()用法及代碼示例
- PHP mysqli_data_seek()用法及代碼示例
- PHP mysqli_insert_id()用法及代碼示例
- PHP mysqli_fetch_assoc()用法及代碼示例
- PHP mkdir()用法及代碼示例
- PHP mysqli_connect_error()用法及代碼示例
- PHP mhash_keygen_s2k()用法及代碼示例
- PHP microtime()用法及代碼示例
- PHP mysqli_fetch_all()用法及代碼示例
- PHP mysqli_next_result()用法及代碼示例
- PHP mysqli_stmt_affected_rows()用法及代碼示例
- PHP mime_content_type()用法及代碼示例
注:本文由純淨天空篩選整理自Malhar Lathkar大神的英文原創作品 PHP mt_rand() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。