{"id":217847,"date":"2023-12-23T01:15:35","date_gmt":"2023-12-22T17:15:35","guid":{"rendered":"https:\/\/www.idc.net\/help\/217847\/"},"modified":"2023-12-23T01:15:35","modified_gmt":"2023-12-22T17:15:35","slug":"php%e5%87%bd%e6%95%b0%ef%bc%9arand","status":"publish","type":"post","link":"https:\/\/idc.net\/help\/217847\/","title":{"rendered":"PHP\u51fd\u6570\uff1arand"},"content":{"rendered":"<p><title id=\"php%e5%87%bd%e6%95%b0%ef%bc%9arand-wFdBpSSvmJ\">PHP\u51fd\u6570\uff1arand<\/title><\/p>\n<h1 id=\"php%e5%87%bd%e6%95%b0%ef%bc%9arand-wFdBpSSvmJ\">PHP\u51fd\u6570\uff1arand<\/h1>\n<p>\u5728PHP\u7f16\u7a0b\u4e2d\uff0crand()\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\u3002\u968f\u673a\u6570\u5728\u5f88\u591a\u5e94\u7528\u4e2d\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u6bd4\u5982\u751f\u6210\u9a8c\u8bc1\u7801\u3001\u968f\u673a\u6392\u5e8f\u3001\u968f\u673a\u9009\u62e9\u7b49\u7b49\u3002<\/p>\n<h2 id=\"%e8%af%ad%e6%b3%95-wFdBpSSvmJ\">\u8bed\u6cd5<\/h2>\n<p>rand()\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code>rand(min, max)<\/code><\/pre>\n<p>\u5176\u4e2d\uff0cmin\u548cmax\u662f\u53ef\u9009\u53c2\u6570\uff0c\u7528\u4e8e\u6307\u5b9a\u751f\u6210\u968f\u673a\u6570\u7684\u8303\u56f4\u3002\u5982\u679c\u4e0d\u6307\u5b9a\u8fd9\u4e24\u4e2a\u53c2\u6570\uff0c\u5219\u9ed8\u8ba4\u751f\u6210\u4e00\u4e2a\u5927\u4e8e\u7b49\u4e8e0\u4e14\u5c0f\u4e8e\u7b49\u4e8eRAND_MAX\u7684\u968f\u673a\u6570\u3002<\/p>\n<h2 id=\"%e7%a4%ba%e4%be%8b-wFdBpSSvmJ\">\u793a\u4f8b<\/h2>\n<p>\u4e0b\u9762\u662f\u4e00\u4e9b\u4f7f\u7528rand()\u51fd\u6570\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code>&lt;?php\n\/\/ \u751f\u6210\u4e00\u4e2a0\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u6570\n$randomNumber = rand(0, 100);\necho \"\u968f\u673a\u6570\uff1a\".$randomNumber;\n?&gt;<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u751f\u6210\u4e00\u4e2a0\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u6570\uff0c\u5e76\u5c06\u5176\u8f93\u51fa\u5230\u5c4f\u5e55\u4e0a\u3002<\/p>\n<h2 id=\"%e5%ba%94%e7%94%a8-wFdBpSSvmJ\">\u5e94\u7528<\/h2>\n<p>rand()\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5f88\u591a\u5b9e\u9645\u5e94\u7528\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u89c1\u7684\u5e94\u7528\u573a\u666f\uff1a<\/p>\n<h3 id=\"%e7%94%9f%e6%88%90%e9%aa%8c%e8%af%81%e7%a0%81-wFdBpSSvmJ\">\u751f\u6210\u9a8c\u8bc1\u7801<\/h3>\n<p>\u5728\u7f51\u7ad9\u6ce8\u518c\u3001\u767b\u5f55\u7b49\u64cd\u4f5c\u4e2d\uff0c\u4e3a\u4e86\u9632\u6b62\u673a\u5668\u4eba\u6076\u610f\u653b\u51fb\uff0c\u901a\u5e38\u4f1a\u4f7f\u7528\u9a8c\u8bc1\u7801\u3002\u9a8c\u8bc1\u7801\u662f\u4e00\u79cd\u968f\u673a\u751f\u6210\u7684\u5b57\u7b26\u6216\u6570\u5b57\uff0c\u7528\u6237\u9700\u8981\u8f93\u5165\u6b63\u786e\u7684\u9a8c\u8bc1\u7801\u624d\u80fd\u7ee7\u7eed\u64cd\u4f5c\u3002rand()\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u968f\u673a\u7684\u9a8c\u8bc1\u7801\u3002<\/p>\n<pre><code>&lt;?php\n\/\/ \u751f\u6210\u4e00\u4e2a\u5305\u542b4\u4e2a\u968f\u673a\u6570\u5b57\u7684\u9a8c\u8bc1\u7801\n$code = \"\";\nfor ($i = 0; $i &lt; 4; $i++) {\n    $code .= rand(0, 9);\n}\necho &quot;\u9a8c\u8bc1\u7801\uff1a&quot;.$code;\n?&gt;<\/code><\/pre>\n<h3 id=\"%e9%9a%8f%e6%9c%ba%e6%8e%92%e5%ba%8f-wFdBpSSvmJ\">\u968f\u673a\u6392\u5e8f<\/h3>\n<p>\u5728\u4e00\u4e9b\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u5bf9\u4e00\u7ec4\u6570\u636e\u8fdb\u884c\u968f\u673a\u6392\u5e8f\uff0c\u4ee5\u589e\u52a0\u7528\u6237\u7684\u4f53\u9a8c\u3002rand()\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u968f\u673a\u6392\u5e8f\u3002<\/p>\n<pre><code>&lt;?php\n\/\/ \u5047\u8bbe\u6709\u4e00\u4e2a\u6570\u7ec4\n$numbers = array(1, 2, 3, 4, 5);\n\/\/ \u968f\u673a\u6392\u5e8f\u6570\u7ec4\nshuffle($numbers);\n\/\/ \u8f93\u51fa\u968f\u673a\u6392\u5e8f\u540e\u7684\u6570\u7ec4\nforeach ($numbers as $number) {\n    echo $number.\" \";\n}\n?&gt;<\/code><\/pre>\n<h3 id=\"%e9%9a%8f%e6%9c%ba%e9%80%89%e6%8b%a9-wFdBpSSvmJ\">\u968f\u673a\u9009\u62e9<\/h3>\n<p>\u5728\u4e00\u4e9b\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u4ece\u4e00\u7ec4\u6570\u636e\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002rand()\u51fd\u6570\u53ef\u4ee5\u7528\u4e8e\u5b9e\u73b0\u968f\u673a\u9009\u62e9\u3002<\/p>\n<pre><code>&lt;?php\n\/\/ \u5047\u8bbe\u6709\u4e00\u4e2a\u6570\u7ec4\n$fruits = array(\"\u82f9\u679c\", \"\u9999\u8549\", \"\u6a59\u5b50\", \"\u8461\u8404\");\n\/\/ \u968f\u673a\u9009\u62e9\u4e00\u4e2a\u6c34\u679c\n$randomFruit = $fruits[rand(0, count($fruits) - 1)];\necho \"\u968f\u673a\u9009\u62e9\u7684\u6c34\u679c\uff1a\".$randomFruit;\n?&gt;<\/code><\/pre>\n<h2 id=\"%e6%80%bb%e7%bb%93-wFdBpSSvmJ\">\u603b\u7ed3<\/h2>\n<p>rand()\u51fd\u6570\u662fPHP\u4e2d\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\u7684\u51fd\u6570\uff0c\u53ef\u4ee5\u7528\u4e8e\u751f\u6210\u9a8c\u8bc1\u7801\u3001\u968f\u673a\u6392\u5e8f\u3001\u968f\u673a\u9009\u62e9\u7b49\u5e94\u7528\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528rand()\u51fd\u6570\uff0c\u53ef\u4ee5\u4e3a\u7f51\u7ad9\u548c\u5e94\u7528\u589e\u52a0\u4e00\u4e9b\u968f\u673a\u6027\uff0c\u63d0\u5347\u7528\u6237\u4f53\u9a8c\u3002<\/p>\n<h3 id=\"%e9%a6%99%e6%b8%af%e6%9c%8d%e5%8a%a1%e5%99%a8%e9%a6%96%e9%80%89%e5%90%8e%e6%b5%aa%e4%ba%91-wFdBpSSvmJ\">\u9999\u6e2f\u670d\u52a1\u5668\u9996\u9009\u540e\u6d6a\u4e91<\/h3>\n<p>\u540e\u6d6a\u4e91\u662f\u4e00\u5bb6\u4e13\u4e1a\u7684\u4e91\u8ba1\u7b97\u516c\u53f8\uff0c\u63d0\u4f9b\u9999\u6e2f\u670d\u52a1\u5668\u3001\u7f8e\u56fd\u670d\u52a1\u5668\u548c\u4e91\u670d\u52a1\u5668\u7b49\u4ea7\u54c1\u3002\u5982\u679c\u60a8\u9700\u8981\u7a33\u5b9a\u53ef\u9760\u7684\u9999\u6e2f\u670d\u52a1\u5668\uff0c\u540e\u6d6a\u4e91\u662f\u60a8\u7684\u9996\u9009\u3002\u8bf7\u8bbf\u95ee<a href=\"https:\/\/idc.net\">https:\/\/idc.net<\/a>\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP\u51fd\u6570\uff1arand PHP\u51fd\u6570\uff1arand \u5728PHP\u7f16\u7a0b\u4e2d\uff0crand()\u51fd\u6570\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u51fd\u6570\uff0c\u7528\u4e8e\u751f\u6210\u968f\u673a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[197851],"tags":[],"class_list":["post-217847","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/217847","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/comments?post=217847"}],"version-history":[{"count":0,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/posts\/217847\/revisions"}],"wp:attachment":[{"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/media?parent=217847"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/categories?post=217847"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/idc.net\/help\/wp-json\/wp\/v2\/tags?post=217847"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}