{"id":10020839,"date":"2025-06-23T17:00:21","date_gmt":"2025-06-23T09:00:21","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020839"},"modified":"2025-06-23T17:00:21","modified_gmt":"2025-06-23T09:00:21","slug":"%e6%b5%8b%e8%af%95-php-%e9%9a%8f%e6%9c%ba%e6%95%b0%e5%87%bd%e6%95%b0%e7%9a%84%e9%9a%8f%e6%9c%ba%e6%80%a7","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020839","title":{"rendered":"\u6d4b\u8bd5 PHP \u968f\u673a\u6570\u51fd\u6570\u7684\u968f\u673a\u6027"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" width=\"723\" height=\"488\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdku8O\" alt=\"image.png\" title=\"image.png\"><br \/>\u968f\u673a\u6570\u751f\u6210\u662f\u6307\u4ea7\u751f\u4e0d\u53ef\u9884\u6d4b\u6570\u5b57\u7684\u8fc7\u7a0b\uff0c\u5728\u8bb8\u591a\u4f9d\u8d56\u968f\u673a\u6027\u7684\u4e0d\u786e\u5b9a\u6027\u5e94\u7528\u4e2d\u53d1\u6325\u7740\u5173\u952e\u4f5c\u7528\u3002\u4e00\u4e2a\u7406\u60f3\u7684\u968f\u673a\u6570\u5e8f\u5217\u4e0d\u5e94\u5177\u5907\u53ef\u9884\u6d4b\u6027\u3002<\/p>\n<p><strong>\u201c\u771f\u6b63\u201d<\/strong>\u7684\u968f\u673a\u6570\u751f\u6210\u53ef\u4ee5\u4f9d\u8d56\u591a\u79cd\u65b9\u5f0f\uff0c\u4f8b\u5982\u63b7\u786c\u5e01\u3001\u63b7\u9ab0\u5b50\uff0c\u751a\u81f3\u57fa\u4e8e\u81ea\u7136\u73b0\u8c61\u7684\u7269\u7406\u8fc7\u7a0b\uff0c\u5982\u5b87\u5b99\u8f90\u5c04\u3001\u5927\u6c14\u538b\u529b\u6216\u7194\u5ca9\u706f\u7684\u53d8\u5316\u7b49\uff0c\u8fd9\u4e9b\u65b9\u6cd5\u5728\u8ba1\u7b97\u673a\u4e2d\u6781\u96be\u88ab\u6a21\u62df\u6216\u9884\u6d4b\u3002<\/p>\n<p>\u4e3a\u6b64\uff0c\u5927\u591a\u6570\u73b0\u4ee3\u64cd\u4f5c\u7cfb\u7edf\u90fd\u5c1d\u8bd5\u63d0\u4f9b\u63a5\u8fd1\u771f\u5b9e\u968f\u673a\u6027\u7684\u751f\u6210\u5668\uff0c\u901a\u5e38\u57fa\u4e8e\u786c\u76d8\u65cb\u8f6c\u5ef6\u8fdf\u7b49\u7cfb\u7edf\u5c42\u9762\u7684\u6d4b\u91cf\u673a\u5236\u3002\u4e3a\u4e86\u63d0\u5347\u5b89\u5168\u6027\uff0c\u7cfb\u7edf\u4f1a\u5b9a\u671f\u91cd\u7f6e\u968f\u673a\u6570\u751f\u6210\u5668\u7684\u79cd\u5b50\uff0c\u4ece\u800c\u9632\u6b62\u56e0\u4f11\u7720\u3001\u5185\u5b58\u8bbf\u95ee\u7b49\u4e8b\u4ef6\u6cc4\u9732\u5185\u90e8\u72b6\u6001\u6216\u88ab\u7ed5\u8fc7\u5b89\u5168\u4fdd\u62a4\u3002<\/p>\n<p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u64cd\u4f5c\u7cfb\u7edf\u4e0e\u7f16\u7a0b\u8bed\u8a00\u7684\u8fd0\u884c\u65f6\u73af\u5883\u901a\u5e38\u5185\u7f6e\u968f\u673a\u6570\u751f\u6210\u5668\uff08RNG\uff09\uff0c\u901a\u8fc7\u7279\u5b9a\u7b97\u6cd5\u7ed3\u5408\u7269\u7406\u71b5\u6e90\u751f\u6210\u4f2a\u968f\u673a\u6570\u3002\u7531\u4e8e\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668\u4f9d\u8d56\u4e00\u4e2a\u521d\u59cb<strong>\u201c\u79cd\u5b50\u201d<\/strong>\uff0c\u4e00\u65e6\u79cd\u5b50\u503c\u4e0e\u7b97\u6cd5\u88ab\u638c\u63e1\uff0c\u6574\u4e2a\u968f\u673a\u5e8f\u5217\u90fd\u53ef\u80fd\u88ab\u9884\u6d4b\u3002\u56e0\u6b64\uff0c\u5728\u5b89\u5168\u6216\u5bf9\u968f\u673a\u6027\u8981\u6c42\u8f83\u9ad8\u7684\u573a\u666f\u4e2d\uff0c\u9009\u62e9\u5408\u9002\u7684\u968f\u673a\u6570\u751f\u6210\u5668\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<h2>PHP \u4e2d\u7684\u968f\u673a\u6570\u751f\u6210\u5668<\/h2>\n<p>PHP \u63d0\u4f9b\u4e86\u51e0\u79cd\u751f\u6210\u968f\u673a\u503c\u7684\u65b9\u6cd5\uff1a<code>rand<\/code>\u3001<code>mt_rand<\/code>\u3001<code>random_int<\/code>\u3001<code>random_bytes<\/code>\u3001<code>openssl_random_pseudo_bytes<\/code> \u7b49\uff0c\u5176\u4e2d\u4e00\u4e9b\u662f\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668\uff0c\u4f46\u5b83\u4eec\u5e26\u6709\u5404\u79cd\u5c5e\u6027\uff0c\u4f7f\u5b83\u4eec\u5f7c\u6b64\u4e0d\u540c\uff0c\u5e76\u4e14\u5fc5\u987b\u4f18\u5148\u4e8e\u53e6\u4e00\u4e2a\u3002<\/p>\n<p>\u968f\u673a\u6570\u751f\u6210\u5668\u65e0\u5904\u4e0d\u5728\uff0c\u4ece\u7b80\u5355\u7684\u7535\u8111\u6e38\u620f\u5230 PIN \u7801\uff0c\u518d\u5230\u4f7f\u7528\u968f\u673a\u751f\u6210\u7684\u52a0\u5bc6\u5bc6\u94a5\u52a0\u5bc6\u7684\u654f\u611f\u4fe1\u606f\u3002\u5728\u5927\u591a\u6570\u5e94\u7528\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u7528\u9002\u5f53\u7684\u968f\u673a\u6570\u751f\u6210\u5668\u81f3\u5173\u91cd\u8981\uff0c\u4ee5\u786e\u4fdd\u6076\u610f\u884c\u4e3a\u8005\u65e0\u6cd5\u786e\u5b9a\u7cfb\u7edf\u5185\u751f\u6210\u7684\u968f\u673a\u6570\u3002<\/p>\n<p>\u6240\u6709\u4f2a\u968f\u673a\u751f\u6210\u5668\u672c\u8d28\u4e0a\u90fd\u4f9d\u8d56\u4e8e\u4e00\u4e2a <strong>\u201c\u79cd\u5b50\u201d<\/strong> \u503c\uff0c\u8be5\u503c\u786e\u5b9a\u4f7f\u7528\u7ed9\u5b9a\u7b97\u6cd5\u751f\u6210\u7684\u6240\u6709\u6574\u4e2a\u968f\u673a\u6570\u5e8f\u5217\u3002\u8ba1\u7b97\u673a\u6e38\u620f\uff08\u5982 Minecraft\uff09\u57fa\u4e8e\u5355\u4e2a\u79cd\u5b50\u503c\u6784\u5efa\u6574\u4e2a\u6e38\u620f\u4e16\u754c\u3002\u4e3a\u4e86\u6784\u5efa\u4e00\u4e2a\u76f8\u540c\u7684\u6e38\u620f\u4e16\u754c\uff0c\u53ea\u9700\u8981\u521d\u59cb\u79cd\u5b50\u3002<\/p>\n<p>PHP \u4e2d\u53ef\u7528\u7684\u968f\u673a\u6570\u751f\u6210\u5668\u5728\u5b9e\u73b0\u65b9\u5f0f\u4ee5\u53ca\u5b83\u4eec\u5728\u4f9d\u8d56\u751f\u6210\u5668\u7684\u968f\u673a\u6027\u6765\u786e\u4fdd\u5b89\u5168\u6027\u7684\u5e94\u7528\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u7684\u5b89\u5168\u6027\u65b9\u9762\u6709\u5f88\u5927\u5dee\u5f02\u3002<\/p>\n<p>\u4ece PHP 8.2 \u5f00\u59cb\uff0cPHP \u63d0\u4f9b\u4e86\u4ee5\u4e0b\u968f\u673a\u6570\u751f\u6210\u5668\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u51fd\u6570\/\u7c7b<\/th>\n<th>\u53ef\u7528\u6027<\/th>\n<th>\u7b14\u8bb0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>rand<\/code><\/td>\n<td>PHP 4 \u53ca\u66f4\u9ad8\u7248\u672c<\/td>\n<td>\u4e0d\u5b89\u5168\uff0c\u4e0d\u9002\u5408\u4efb\u4f55\u4e0e\u5b89\u5168\u76f8\u5173\u7684\u5e94\u7528\u7a0b\u5e8f\u3002\u81ea PHP 7.1 \u8d77\u4e3a <code>mt_rand<\/code> \u7684\u522b\u540d<\/td>\n<\/tr>\n<tr>\n<td><code>mt_rand<\/code><\/td>\n<td>PHP 4 \u53ca\u66f4\u9ad8\u7248\u672c<\/td>\n<td>\u4f7f\u7528 Mersenne Twister\uff0c\u5728 PHP 7.1 \u4e2d\u6709\u6240\u6539\u8fdb\uff0c\u4f46\u672c\u8d28\u4e0a\u4ecd\u4e0d\u5b89\u5168<\/td>\n<\/tr>\n<tr>\n<td><code>lcg_value<\/code><\/td>\n<td>PHP 4 \u53ca\u66f4\u9ad8\u7248\u672c<\/td>\n<td>\u8fd4\u56de 0 \u5230 1 \u4e4b\u95f4\u7684\u6d6e\u70b9\u6570\uff08\u5305\u62ec 0 \u548c 1\uff09\uff0c\u52a0\u5bc6\u4e0d\u5b89\u5168<\/td>\n<\/tr>\n<tr>\n<td><code>random_int<\/code><\/td>\n<td>PHP 7.0 \u53ca\u66f4\u9ad8\u7248\u672c<\/td>\n<td><strong>\u63a8\u8350\u4f7f\u7528<\/strong>\uff0c\u56e0\u4e3a\u5b83\u5728\u52a0\u5bc6\u4e0a\u662f\u5b89\u5168\u7684<\/td>\n<\/tr>\n<tr>\n<td><code>random_bytes<\/code><\/td>\n<td>PHP 7.0 \u53ca\u66f4\u9ad8\u7248\u672c<\/td>\n<td><strong>\u63a8\u8350\u4f7f\u7528<\/strong>\uff0c\u5982\u679c\u7cfb\u7edf\u71b5\u4e0d\u8db3\u4f1a\u5931\u8d25\uff0c\u4e0d\u4f1a\u56de\u9000\u5230\u4e0d\u5b89\u5168\u7b97\u6cd5<\/td>\n<\/tr>\n<tr>\n<td><code>openssl_random_pseudo_bytes<\/code><\/td>\n<td>OpenSSL \u548c PHP 5.3 \u53ca\u66f4\u9ad8\u7248\u672c<\/td>\n<td>\u53ef\u7528\u4e8e\u83b7\u53d6\u52a0\u5bc6\u5b89\u5168\u7684\u968f\u673a\u6570\uff0c\u4f46\u4e0d\u80fd\u4fdd\u8bc1\uff0c\u56e0\u4e3a\u8be5\u51fd\u6570\u53ef\u80fd\u4f1a\u56de\u9000\u5230\u4e0d\u5b89\u5168\u7684\u7b97\u6cd5<\/td>\n<\/tr>\n<tr>\n<td><code>Random\/Engine\/Mt19937<\/code><\/td>\n<td>PHP 8.2 \u53ca\u66f4\u9ad8\u7248\u672c<\/td>\n<td><code>mt_rand<\/code> \u7684\u9762\u5411\u5bf9\u8c61\u63a5\u53e3<\/td>\n<\/tr>\n<tr>\n<td><code>Random\/Engine\/PcgOneseq128XslRr64<\/code><\/td>\n<td>PHP 8.2 \u53ca\u66f4\u9ad8\u7248\u672c<\/td>\n<td><a href=\"https:\/\/link.segmentfault.com\/?enc=KPEFKlk%2B%2FbocK6pSDx761w%3D%3D.F2eo8NV8N%2BHtavNKGy9w%2B%2FKuGay7sLoRfXnBaJ9OuoaRNZeAPZATLsjkS%2BP66pR30sm%2B6OV9cQ79mZk9RxOKRi9kai4K%2BIVPjCdIFSX3pYAUR8XpOhqR3FsbSEeAQBVE\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">\u7f6e\u6362\u540c\u4f59\u751f\u6210\u5668<\/a>\u5b9e\u73b0<\/td>\n<\/tr>\n<tr>\n<td><code>Random\/Engine\/Xoshiro256StarStar<\/code><\/td>\n<td>PHP 8.2 \u53ca\u66f4\u9ad8\u7248\u672c<\/td>\n<td><a href=\"https:\/\/link.segmentfault.com\/?enc=R8m6BzF%2BaNBpOWPsuyLZvQ%3D%3D.7V4BU%2BQCew4%2B4AoPcZjUnwa07p9n9nSrbFp1YeEkes%2F0cp5eQZung0jbEdCpMiDv%2BV7xddwEEs0hLUH3gYpHOA%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Xoshiro<\/a> PRNG\uff08\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668\uff09\u5b9e\u73b0<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>\u6d4b\u91cf PHP PRNGs \u4e2d\u7684\u968f\u673a\u6027<\/h2>\n<p>\u6d4b\u91cf\u968f\u673a\u6570\u751f\u6210\u5668\u968f\u673a\u6027\u7684\u6700\u7b80\u5355\u65b9\u6cd5\u4e4b\u4e00\u662f\u53ef\u89c6\u5316\u4ea7\u751f\u7684\u503c\u4ee5\u89c2\u5bdf\u6a21\u5f0f\u3002<\/p>\n<p>\u4f7f\u7528 PHP \u7684 GD \u6269\u5c55\uff0c\u53ef\u4ee5\u901a\u8fc7\u5728\u968f\u673a\u7684 X \u548c Y \u5750\u6807\u4e0a\u653e\u7f6e\u5355\u4e2a\u50cf\u7d20\u6765\u7ed8\u5236\u56fe\u50cf\u3002\u5728\u9884\u5148\u786e\u5b9a\u7684\u5c1d\u8bd5\u6b21\u6570\u4e0a\uff0c\u50cf\u7d20\u5206\u5e03\u5927\u81f4\u5747\u5300\u7684\u56fe\u50cf\u8868\u793a\u968f\u673a\u6570\u751f\u6210\u5668\u63a5\u8fd1<strong>\u201c\u771f\u5b9e\u201d<\/strong>RNG\u3002\u5982\u679c\u79cd\u5b50\u4fdd\u6301\u4e0d\u53d8\uff0c\u5219\u7eaf\u7cb9\u57fa\u4e8e\u79cd\u5b50\u503c\u751f\u6210\u503c\u7684 RNG\uff08\u4f8b\u5982 Mersenne Twister\uff09\u5e94\u751f\u6210\u76f8\u540c\u7684\u8f93\u51fa\u3002<\/p>\n<pre><code class=\"php\">\/\/ \u5c06\u6d4b\u8bd5\u51fd\u6570\u547d\u540d\u4e3a\u8f93\u51fa\u56fe\u50cf\u6587\u4ef6\u7684\u540d\u79f0\n$test_name = 'random_int';\n$size = 250;\n\n$random_function = static function(int $max) {\n    return random_int(0, $max);\n};\n\n\/\/ \u521b\u5efa\u4e00\u4e2a\u6307\u5b9a $size \u7684\u7a7a\u767d\u6b63\u65b9\u5f62\u56fe\u50cf\n$im = imagecreatetruecolor($size, $size);\n$white = imagecolorallocate($im, 255, 255, 255);\n$black = imagecolorallocate($im, 0, 0, 0);\n\/\/ \u5c06\u56fe\u50cf\u586b\u5145\u4e3a\u767d\u8272\nimagefill($im, 0, 0, $white);\n\n$iterations = $size ** 2;\n\nfor ($i = 0; $i &lt; $iterations; $i++) {\n    $x = $random_function($size);\n    $y = $random_function($size);\n    imagesetpixel($im, $x, $y, $black);\n}\n\nimagepng($im, $test_name . '.png', 0);\nimagedestroy($im);<\/code><\/pre>\n<p><code>random_int<\/code> \u51fd\u6570\u662f CSPRNG\uff08\u52a0\u5bc6\u5b89\u5168\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668\uff09\uff0c\u81ea PHP 7.0 \u8d77\u53ef\u7528\u3002\u5982\u679c\u5b83\u65e0\u6cd5\u4ea7\u751f\u5177\u6709\u8db3\u591f\u968f\u673a\u6027\u7684\u6570\u5b57\u7684\u8870\u8001\uff0c\u5219\u4f1a\u5f15\u53d1 Exception\uff0c\u5e76\u4e14\u4e0d\u4f1a\u56de\u9000\u5230\u4efb\u4f55\u4e0d\u5b89\u5168\u7684 RNG \u6e90\u3002<\/p>\n<p>\u6b64\u5916\uff0c\u65e0\u6cd5\u66f4\u6539\u6b64 RNG \u7684\u79cd\u5b50\u503c\uff0c\u8fd9\u662f\u5728 PHP \u4e2d\u83b7\u53d6\u968f\u673a\u6570\u548c\u5b57\u8282\u7684\u63a8\u8350\u65b9\u6cd5\uff0c\u5373\u4f7f\u5bf9\u4e8e\u52a0\u5bc6\u4f5c\u4e5f\u662f\u5982\u6b64\u3002<\/p>\n<h2>random_int \/ random_bytes<\/h2>\n<pre><code class=\"php\">$random_function = static function(int $max) {\n    return random_int(0, $max);\n};<\/code><\/pre>\n<p>\u4f7f\u7528\u4e0a\u9762\u7684\u4ee3\u7801\u6bb5\u6765\u53ef\u89c6\u5316\u968f\u673a\u6027\uff0c<code>random_int<\/code> \u51fd\u6570\u4f1a\u751f\u6210\u7c7b\u4f3c\u4e8e\u4ee5\u4e0b\u5185\u5bb9\u7684\u56fe\u50cf\uff1a<\/p>\n<p><img decoding=\"async\" width=\"250\" height=\"250\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdku9X\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/><strong><em>\u201crandom_int\u201d<\/strong>\u51fd\u6570\u968f\u673a\u6027\u7684\u53ef\u89c6\u5316\uff0c\u663e\u793a\u4ece RNG \u8fd4\u56de\u7684\u5747\u5300\u5206\u5e03\u503c<\/em><\/p>\n<blockquote><p>PHP 8.2 \u7684\u65b0\u529f\u80fd: \u5728 PHP 8.2 \u4e2d\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528 <code>Random\/Engine\/Secure<\/code> \u5f15\u64ce\u5c06 <code>random_int<\/code> \u548c <code>random_bytes PRNG<\/code> \u4e0e <code>scoped<\/code> \u548c <code>OOP API<\/code> \u4e00\u8d77\u4f7f\u7528\u3002<a href=\"https:\/\/link.segmentfault.com\/?enc=eX76OLeuQB%2FBJ63r8PPFDw%3D%3D.0z%2BRDky7SdKqiIHT7K8LnuypxaSHC9DSxr0lB5ff7RLJbnpIIFsVs5ey4%2Ft%2FoOFqXwjX7nM6m4p4CrBAKFERxA%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">PHP 8.2 \u4e2d\u6709\u66f4\u8be6\u7ec6\u7684\u793a\u4f8b\uff1a\u65b0\u7684\u968f\u673a\u6269\u5c55 &#8211; \u4f7f\u7528\u793a\u4f8b<\/a><\/p><\/blockquote>\n<h2>mt_rand \u51fd\u6570\u548c Random\/Engine\/Mt19937 \u5f15\u64ce<\/h2>\n<p>Mersenne Twister \u662f PHP \u4e2d\u53ef\u7528\u7684\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668\u3002\u5b83\u7684 seed \u503c\u662f\u53ef\u914d\u7f6e\u7684\uff0c\u4f46 PHP \u5728\u5f00\u59cb\u65f6\u4f1a\u7528\u8db3\u591f\u968f\u673a\u7684\u79cd\u5b50\u6765\u8bbe\u5b9a\u5b83\u7684\u79cd\u5b50\u3002<\/p>\n<pre><code class=\"php\">$random_function = static function(int $max) {\n    return mt_rand(0, $max);\n};<\/code><\/pre>\n<p><img decoding=\"async\" width=\"250\" height=\"250\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdkvaa\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<p><strong><em>\u201cmt_rand\u201d<\/strong>\u51fd\u6570\u968f\u673a\u6027\u7684\u53ef\u89c6\u5316\uff0c\u663e\u793a\u4ece RNG \u8fd4\u56de\u7684\u5747\u5300\u5206\u5e03\u503c<\/em><\/p>\n<p>Mersenne Twister RNG \u4e0e\u6df7\u5408\/\u786c\u4ef6\u65b9\u6cd5\u4e4b\u95f4\u7684\u4e00\u4e2a\u4e3b\u8981\u533a\u522b\u662f\u968f\u673a\u6570\u7684\u6574\u4e2a\u5e8f\u5217\u90fd\u53d6\u51b3\u4e8e\u79cd\u5b50\u3002PHP \u7684 <code>mt_srand<\/code> \u7528\u4e8e\u4e3a RNG \u8bbe\u5b9a\u79cd\u5b50\u3002Mersenne Twister \u4e3a\u540c\u4e00\u79cd\u5b50\u751f\u6210\u76f8\u540c\u7684\u968f\u673a\u6570\u5e8f\u5217\u3002<\/p>\n<p>\u5f53\u5e94\u7528\u7a0b\u5e8f\/\u6e38\u620f\u9700\u8981\u57fa\u4e8e\u5355\u4e2a\u79cd\u5b50\uff08\u4f8b\u5982 Minecraft\uff09\u751f\u6210\u503c\u65f6\uff0c\u8fd9\u79cd\u786e\u5b9a\u6027\u6027\u8d28\u975e\u5e38\u6709\u7528\u3002\u4f46\u662f\uff0c\u5bf9\u4e8e\u9700\u8981\u751f\u6210\u968f\u673a\u6570\u4ee5\u8fdb\u884c\u52a0\u5bc6\u7684\u5e94\u7528\u7a0b\u5e8f\u6765\u8bf4\uff0cMersenne Twister \u662f\u4e00\u4e2a\u7cdf\u7cd5\u7684\u9009\u62e9\uff0c\u56e0\u4e3a RNG \u7684\u5185\u90e8\u72b6\u6001\u53ef\u4ee5\u901a\u8fc7\u89c2\u5bdf RNG \u4ea7\u751f\u7684\u968f\u673a\u6570\u5e8f\u5217\u6765\u63a8\u5bfc\u51fa\u6765\u3002<\/p>\n<p>\u4e3a\u4e86\u53ef\u89c6\u5316\u8fd9\u4e00\u70b9\uff0c\u4f7f\u7528 <code>mt_rand()<\/code> \u51fd\u6570\u751f\u6210\u4ee5\u4e0b\u4e09\u4e2a\u3002\u5176\u4e2d\u4e24\u4e2a\u4f7f\u7528 $seed = 42\uff0c\u6700\u540e\u4e00\u4e2a\u4f7f\u7528 $seed = 43\uff1a<\/p>\n<pre><code class=\"php\">mt_srand($seed);\n$random_function = static function(int $max) {\n    return mt_rand(0, $max);\n};<\/code><\/pre>\n<p>\u4e3a\u4e86\u66f4\u597d\u5730\u53ef\u89c6\u5316\u76f8\u4f3c\u4e4b\u5904\u548c\u4e0d\u540c\u4e4b\u5904\uff0c\u4f7f\u7528 <code>imagefilledrectangle\uff08\uff09<\/code> \u51fd\u6570\u751f\u6210\u4e86\u4ee5\u4e0b\u56fe\u50cf\uff0c\u4ee5\u7ed8\u5236\u586b\u5145\u77e9\u5f62\u800c\u4e0d\u662f\u5355\u4e2a\u50cf\u7d20\u3002\u77e9\u5f62\u7684\u989c\u8272\u4e5f\u662f\u968f\u673a\u9009\u62e9\u7684\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>\u79cd\u5b50\u503c\u793a\u4f8b<\/th>\n<th>\u8bf4\u660e<\/th>\n<th>\u56fe\u793a<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>$seed = 42\uff08\u7b2c\u4e00\u6b21\u8fd0\u884c\uff09  <\/code><\/td>\n<td>\u53ea\u8981\u79cd\u5b50\u503c\u76f8\u540c\uff0c\u751f\u6210\u7684\u968f\u673a\u6570\u5e8f\u5217\u5c31\u4fdd\u6301\u4e0d\u53d8\u3002<\/td>\n<td><img decoding=\"async\" width=\"150\" height=\"150\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdkva9\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/td>\n<\/tr>\n<tr>\n<td><code>$seed = 42\uff08\u7b2c\u4e8c\u6b21\u8fd0\u884c\uff09<\/code><\/td>\n<td>\u53ea\u8981\u79cd\u5b50\u503c\u76f8\u540c\uff0c\u751f\u6210\u7684\u968f\u673a\u6570\u5e8f\u5217\u5c31\u4fdd\u6301\u4e0d\u53d8\u3002<\/td>\n<td><img decoding=\"async\" width=\"150\" height=\"150\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdkva9\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/td>\n<\/tr>\n<tr>\n<td><code>$seed = 43<\/code><\/td>\n<td>\u5373\u4f7f\u66f4\u6539\u79cd\u5b50\u503c\u4e00\u4e2a\u5b57\u8282\u4e5f\u4f1a\u4ea7\u751f\u5b8c\u5168\u4e0d\u540c\u7684\u968f\u673a\u6570\u7cfb\u5217\u3002<\/td>\n<td><img decoding=\"async\" width=\"150\" height=\"150\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdkva4\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2>rand \u529f\u80fd<\/h2>\n<p>\u4ece PHP 7.1 \u5f00\u59cb\uff0cPHP \u7684 <code>rand<\/code> \u51fd\u6570\u5728\u540e\u53f0\u4f7f\u7528 MT\uff0c\u7ed3\u679c\u4e0e <code>mt_rand<\/code> \u51fd\u6570\u76f8\u540c\u3002\u7136\u800c\uff0c\u5728 PHP 7.0 \u4e4b\u524d\uff0c<code>rand<\/code> \u51fd\u6570\u4ea7\u751f\u4e86\u66f4\u53ef\u9884\u6d4b\u548c\u91cd\u590d\u7684\u968f\u673a\u6570\u6d41\uff0c\u8fd9\u4f7f\u5f97\u5b83\u4e0e\u771f\u6b63\u7684\u968f\u673a\u6570\u751f\u6210\u5668\u76f8\u53bb\u751a\u8fdc\u3002<\/p>\n<pre><code class=\"php\">$random_function = static function(int $max) {\n    return rand(0, $max);\n};<\/code><\/pre>\n<p><img decoding=\"async\" width=\"250\" height=\"250\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdkvbd\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/><em>\u53ef\u89c6\u5316 &#8216;rand&#8217; \u51fd\u6570\uff0c\u5177\u6709\u53ef\u89c1\u7684\u53ef\u9884\u6d4b\u6a21\u5f0f<\/em><\/p>\n<p>\u5728 PHP 7.1 \u53ca\u66f4\u9ad8\u7248\u672c\u4e2d\u4f7f\u7528 <code>rand<\/code> \u5fc5\u7136\u662f\u574f\u7684\uff0c\u56e0\u4e3a\u5b83\u65e0\u8bba\u5982\u4f55\u90fd\u4f1a\u4f7f\u7528 <code>mt_rand<\/code>\u3002\u4f46\u662f\uff0c\u8981\u786e\u4fdd\u9759\u6001\u5206\u6790\u5668\u4e0d\u4f1a\u6807\u8bb0 <code>rand<\/code> \u7684\u4f7f\u7528\uff0c\u8bf7\u8003\u8651\u8fc1\u79fb\u5230 <code>random_int<\/code>\u3002<\/p>\n<h2>lcg_value \u529f\u80fd<\/h2>\n<p><code>lcg_value()<\/code> \u662f\u4e00\u4e2a<a href=\"https:\/\/link.segmentfault.com\/?enc=KnlqECYX8V3jN8CPQ0xyKQ%3D%3D.HA8cqpS2YqsCRg%2BfM9kd6SYHWvsGoq1u3uXFjRNV4NU8lK%2FcpXHPjppOW3MqnQiKModYOh0WZgdZubeDRRRbEAgafe2%2FEu6cn7tiABrIUjc%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">\u7ec4\u5408\u7ebf\u6027\u540c\u4f59\u751f\u6210\u5668<\/a> \uff0c\u5b83\u8fd4\u56de\u4e00\u4e2a\u4ecb\u4e8e 0 \u548c 1 \u4e4b\u95f4\u7684\u4f2a\u968f\u673a\u6570\u3002\u6b64\u51fd\u6570\u4e5f\u4e0d\u88ab\u89c6\u4e3a\u52a0\u5bc6\u5b89\u5168\u7684\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668\u3002<\/p>\n<pre><code class=\"php\">$random_function = static function(int $max) {\n    return abs((int) (lcg_value() * $max));\n};<\/code><\/pre>\n<p><img decoding=\"async\" width=\"250\" height=\"250\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdkvbh\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<p><em>\u53ef\u89c6\u5316<strong>\u201clcg_value\u201d<\/strong>\u529f\u80fd\uff0c\u5177\u6709\u770b\u4f3c\u968f\u673a\u7684\u566a\u58f0\u4e14\u6ca1\u6709\u53ef\u89c1\u7684\u6a21\u5f0f<\/em><\/p>\n<h2>Random\/Engine\/PcgOneseq128XslRr64 \u548c Random\/Engine\/Xoshiro256StarStar<\/h2>\n<p>\u5728 PHP 8.2 \u4e2d\uff0c\u5927\u591a\u6570 RNG \u51fd\u6570\u88ab\u79fb\u52a8\u5230\u4e00\u4e2a\u540d\u4e3a random \u7684\u65b0\u6269\u5c55\u4e2d\u3002\u5b83\u5f15\u5165\u4e86\u4e00\u4e2a\u65b0\u7684 <code>\/Random\/Randomizer<\/code> \u7c7b\uff0c\u4ee5\u53ca\u56db\u4e2a\u4f5c\u4e3a PHP \u7c7b\u7684 RNG \u5f15\u64ce\u3002<\/p>\n<p>\u4f7f\u7528\u65b0\u7684\u968f\u673a\u6269\u5c55\u6dfb\u52a0\u7684\u4e24\u4e2a\u65b0 PRNG \u662f PcgOneseq128XslRr64\uff08<a href=\"https:\/\/link.segmentfault.com\/?enc=UbRvzVhbcF2a5eBJK9Bc3w%3D%3D.o0FRpL5gmVHiY0dlfjHOPyA6EmxDw9S%2BUCevvwf9zYVJqSEV4WN1yctA95d92u9SvR6Y7umBHAZ5NPAjFOadZw%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">\u7f6e\u6362\u540c\u4f59\u751f\u6210\u5668<\/a>\u5b9e\u73b0\uff09\u548c Xoshiro256StarStar\uff08Xoshiro PRNG \u5b9e\u73b0\uff09\u3002\u5b83\u4eec\u90fd\u7c7b\u4f3c\u4e8e Mersenne Twister\uff0c\u56e0\u4e3a\u5b83\u4eec\u90fd\u53ea\u4f9d\u8d56\u4e8e\u521d\u59cb\u79cd\u5b50\u503c\uff0c\u53ea\u8981\u79cd\u5b50\u503c\u4fdd\u6301\u4e0d\u53d8\uff0c\u5c31\u53ef\u4ee5\u7528\u4f5c\u786e\u5b9a\u6027\u6570\u5b57\u6d41\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th>PRNG \u7b97\u6cd5<\/th>\n<th>$seed = 42\uff08\u7b2c\u4e00\u6b21\u8fd0\u884c\uff09<\/th>\n<th>$seed = 42\uff08\u7b2c\u4e8c\u6b21\u8fd0\u884c\uff09<\/th>\n<th>$seed = 43<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>\u53ef\u89c6\u5316\u8bf4\u660e<\/td>\n<td>\u53ea\u8981\u79cd\u5b50\u503c\u76f8\u540c\uff0c\u751f\u6210\u7684\u968f\u673a\u6570\u5e8f\u5217\u5c31\u4fdd\u6301\u4e0d\u53d8\u3002<\/td>\n<td>\u53ea\u8981\u79cd\u5b50\u503c\u76f8\u540c\uff0c\u751f\u6210\u7684\u968f\u673a\u6570\u5e8f\u5217\u5c31\u4fdd\u6301\u4e0d\u53d8\u3002<\/td>\n<td>\u5373\u4f7f\u66f4\u6539\u79cd\u5b50\u503c\u4e00\u4e2a\u5b57\u8282\u4e5f\u4f1a\u4ea7\u751f\u5b8c\u5168\u4e0d\u540c\u7684\u968f\u673a\u6570\u7cfb\u5217\u3002<\/td>\n<\/tr>\n<tr>\n<td>PcgOneseq128XslRr64<\/td>\n<td><img decoding=\"async\" width=\"100\" height=\"100\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdkvbz\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/td>\n<td><img decoding=\"async\" width=\"100\" height=\"100\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdkvbz\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/td>\n<td><img decoding=\"async\" width=\"100\" height=\"100\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdkvbA\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/td>\n<\/tr>\n<tr>\n<td>Xoshiro256StarStar<\/td>\n<td><img decoding=\"async\" width=\"100\" height=\"100\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdkvbB\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/td>\n<td><img decoding=\"async\" width=\"100\" height=\"100\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdkvbB\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/td>\n<td><img decoding=\"async\" width=\"100\" height=\"100\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVdkvbC\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>PHP \u63d0\u4f9b\u4e86\u51e0\u4e2a\u968f\u673a\u6570\u751f\u6210\u5668\uff0c\u9009\u62e9\u6b63\u786e\u7684\u968f\u673a\u6570\u751f\u6210\u5668\u5f88\u91cd\u8981\uff0c\u5c24\u5176\u662f\u5f53\u6d89\u53ca\u5230\u968f\u673a\u6570\u6d41\u7684\u4e0d\u53ef\u9884\u6d4b\u6027\u5f88\u91cd\u8981\u7684\u5e94\u7528\u7a0b\u5e8f\u65f6\u3002\u4e00\u4e9b RNG\uff0c\u6bd4\u5982 PHP 7.1 \u4e4b\u524d\u7684 <code>rand()<\/code> \u672c\u8d28\u4e0a\u662f\u4e0d\u5b89\u5168\u548c\u53ef\u9884\u6d4b\u7684\u3002<\/p>\n<p>PHP 8.2 \u9664\u4e86\u73b0\u6709\u7684 Mersenne Twister\uff08\u5e26\u6709\u5e26\u6709 <a href=\"https:\/\/link.segmentfault.com\/?enc=zABaYk9pG04LEZ1lJLmUKg%3D%3D.twZ5aqRF2cn9%2F9r%2BmgcHZiUGPdmwFMM%2BwKlsi2RnjBbbof4hQUaDOyZPIvR%2BrDc%2FSRz9%2BaAK3D20uA5C%2FTLQ7g%3D%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Mt19937<\/a> \u7684 OOP API\uff09\u4e4b\u5916\uff0c\u8fd8\u63d0\u4f9b <a href=\"https:\/\/link.segmentfault.com\/?enc=QM2wCx%2BtWPIjqbZK8hdv3w%3D%3D.m53HJaRL5EkH5mj2BdqE5mKsY75kDPxhNvgNxJdCAabQw0iJFmDJTEx9T%2FJCb8GYx%2F56HV%2FmE0gbuog1nd5763rWOa3%2FB6kfij%2Bx2wPV3EU%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Xoshiro256StarStar<\/a> \u548c <a href=\"https:\/\/link.segmentfault.com\/?enc=zMKGYkun3LiBNWc2SFCDDA%3D%3D.4gQIKP%2BUwkrXcvyS3N0hypjuKsgH8H3FC%2Fchx%2FbRF1OY%2F1dzC%2FZui0iXIT3eyn7%2BqQSsNxvOOs8ckMMk0hRykdlX1TJKwQzwyHUYAcRkXK4%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">PcgOneseq128XslRr64<\/a> \u4f5c\u4e3a\u4e24\u4e2a\u989d\u5916\u7684 RNG\u3002<\/p>\n<p>\u5e76\u975e\u6240\u6709 RNG \u90fd\u9002\u7528\u4e8e\u52a0\u5bc6\u5b89\u5168\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668 \uff08CSPRNG\uff09\u3002\u5728 PHP 7.0 \u4e4b\u524d\uff0cOpenSSL \u6269\u5c55\u7684 <code>openssl_random_pseudo_bytes<\/code> \u51fd\u6570\u662f\u66f4\u597d\u7684\u9009\u62e9\uff0c\u4f46\u5373\u4f7f\u8fd9\u6837\u4e5f\u4e0d\u7406\u60f3\uff0c\u56e0\u4e3a\u5982\u679c\u4e0d\u53ef\u80fd\u968f\u673a\u6570\u5177\u6709\u8db3\u591f\u7684\u71b5\uff0c\u5b83\u901a\u5e38\u4f1a\u8fd4\u56de false\u3002\u6b64\u95ee\u9898\u540e\u6765\u5728 PHP 7.4 \u4e2d\u5f97\u5230\u4fee\u590d\uff0c\u5728 RNG \u5931\u8d25\u65f6\u629b\u51fa\u5f02\u5e38\u3002<\/p>\n<p>Mersenne Twister\u3001Xoshiro256StarStar \u548c PcgOneseq128XslRr64 \u7b49 RNG \u4e5f\u6709\u7528\u4f8b\uff0c\u4f46\u7531\u4e8e\u5b83\u4eec\u4f9d\u8d56\u4e8e\u5355\u4e2a\u79cd\u5b50\u503c\uff0c\u56e0\u6b64\u4e0d\u5efa\u8bae\u5c06\u5b83\u4eec\u7528\u4e8e\u4efb\u4f55\u9700\u8981 CSPRNG \u7684\u5e94\u7528\u7a0b\u5e8f\u3002<\/p>\n<blockquote><p>\u5bf9\u4e8e\u6240\u6709\u968f\u673a\u6570\u751f\u6210\u76ee\u7684\uff0c\u5efa\u8bae\u4f7f\u7528 <code>random_int<\/code> \u548c <code>random_bytes<\/code> \u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u4f1a\u5931\u8d25\u5e76\u663e\u793a\u5f02\u5e38\uff08\u800c\u4e0d\u662f\u9759\u9ed8\u9ed8\u8ba4\u4e0d\u5b89\u5168\u7684\u7b97\u6cd5\uff09\uff0c\u5e76\u4e14\u81ea PHP 7.0 \u4ee5\u6765\u7684\u6240\u6709 PHP \u7248\u672c\u90fd\u652f\u6301\u5b83\u3002<\/p><\/blockquote>\n<p>\u4e0d\u4f7f\u7528 RNG \u8fdb\u884c\u52a0\u5bc6\u4f7f\u7528\u7684\u5e94\u7528\u7a0b\u5e8f\u53ef\u4ee5\u4f7f\u7528\u66f4\u5feb\u7684\u5b9e\u73b0\uff0c\u4f8b\u5982 <a href=\"https:\/\/link.segmentfault.com\/?enc=RL9NKpSKeEtnZpYok9teqQ%3D%3D.UI%2FN9%2B4f7am8ykX33TOj7cJDCdPiUE0nYC3kJQH4TjE5ztnclngIUJoXDP6BvaMsMQ14sDYOz0mNN6dB4S6rUx2s91ll5iNubeil7OJ8BwE%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Xoshiro256StarStar<\/a>\u3002<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046793130\">https:\/\/segmentfault.com\/a\/1190000046793130<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u968f\u673a\u6570\u751f\u6210\u662f\u6307\u4ea7\u751f\u4e0d\u53ef\u9884\u6d4b\u6570\u5b57\u7684\u8fc7\u7a0b\uff0c\u5728\u8bb8\u591a\u4f9d\u8d56\u968f\u673a\u6027\u7684\u4e0d\u786e\u5b9a\u6027\u5e94\u7528\u4e2d\u53d1\u6325\u7740\u5173\u952e\u4f5c\u7528\u3002\u4e00\u4e2a\u7406\u60f3\u7684\u968f\u673a\u6570\u5e8f\u5217\u4e0d\u5e94 [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020839","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020839","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020839"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020839\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020839"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020839"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020839"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}