{"id":265384,"date":"2024-05-15T16:10:28","date_gmt":"2024-05-15T08:10:28","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/265384.html"},"modified":"2024-05-15T16:10:35","modified_gmt":"2024-05-15T08:10:35","slug":"%e5%89%8d%e7%ab%af-javascript-%e4%bb%a3%e7%a0%81%e6%80%8e%e4%b9%88%e5%88%9b%e5%bb%ba%e9%9a%8f%e6%9c%ba%e6%95%b4%e6%95%b0-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/265384.html","title":{"rendered":"\u524d\u7aef JavaScript \u4ee3\u7801\u600e\u4e48\u521b\u5efa\u968f\u673a\u6574\u6570"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27130116\/aa45b0be-2743-4009-a1c4-dc44e6e0aadf.webp\" alt=\"\u524d\u7aef JavaScript \u4ee3\u7801\u600e\u4e48\u521b\u5efa\u968f\u673a\u6574\u6570\" \/><\/p>\n<p><p>\u8981\u5728\u524d\u7aefJavaScript\u4e2d\u521b\u5efa\u968f\u673a\u6574\u6570\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 <code>Math.random()<\/code> \u65b9\u6cd5\u7ed3\u5408 <code>Math.floor()<\/code> \u6216 <code>Math.ceil()<\/code> \u65b9\u6cd5\u3002\u4e00\u4e2a\u57fa\u7840\u7684\u4f8b\u5b50\u662f\uff1a<code>Math.floor(Math.random() * (\u6700\u5927\u503c - \u6700\u5c0f\u503c + 1)) + \u6700\u5c0f\u503c<\/code>\uff0c\u8fd9\u6837\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u4ecb\u4e8e\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u4e4b\u95f4\uff08\u542b\u4e24\u7aef\uff09\u7684\u968f\u673a\u6574\u6570\u3002<strong>\u4f7f\u7528<code>Math.random()<\/code>\u51fd\u6570\u751f\u6210\u7684\u968f\u673a\u6570\u662f\u4f2a\u968f\u673a\u6570\uff0c\u5e76\u4e14\u5b83\u662f\u5747\u5300\u5206\u5e03\u7684\uff0c\u8fd9\u610f\u5473\u7740\u6bcf\u4e2a\u6570\u503c\u7684\u51fa\u73b0\u673a\u7387\u5927\u4f53\u4e0a\u662f\u76f8\u7b49\u7684<\/strong>\u3002<\/p>\n<\/p>\n<p><p>\u8ba9\u6211\u4eec\u66f4\u8be6\u7ec6\u5730\u4e86\u89e3\u8fd9\u4e2a\u65b9\u6cd5\u548c\u5b83\u7684\u5b9e\u73b0\u7ec6\u8282\u3002<code>Math.random()<\/code>\u65b9\u6cd5\u4ea7\u751f\u4e00\u4e2a\u5927\u4e8e\u7b49\u4e8e0\u5c0f\u4e8e1\u7684\u6d6e\u70b9\u6570\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3a[0, 1)\u533a\u95f4\u7684\u968f\u673a\u6570\u3002\u800c<code>Math.floor()<\/code>\u4f1a\u5411\u4e0b\u53d6\u6574\uff0c\u5373\u5b83\u4f1a\u8fd4\u56de\u5c0f\u4e8e\u6216\u7b49\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u6570\u5b57\u7684\u6700\u5927\u6574\u6570\u3002\u5728\u9700\u8981\u751f\u6210\u7279\u5b9a\u8303\u56f4\u5185\u7684\u6574\u6570\u65f6\uff0c\u8fd9\u79cd\u7ec4\u5408\u7279\u522b\u6709\u7528\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u7406\u89e3<code>Math.random()<\/code><\/h3>\n<\/p>\n<p><p><strong>Math.random()<\/strong> \u65b9\u6cd5\u4ea7\u751f\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u4f2a\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u4e0d\u5305\u62ec1\u3002\u6bcf\u6b21\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\uff0c\u5b83\u51e0\u4e4e\u4e0d\u53ef\u80fd\u8fd4\u56de\u76f8\u540c\u6216\u8005\u7279\u5b9a\u7684\u6570\u503c\uff0c\u8fd9\u662f\u56e0\u4e3a\u5b83\u662f\u8bbe\u8ba1\u6765\u5c3d\u53ef\u80fd\u5730\u6a21\u62df\u771f\u968f\u673a\u6027\u3002\u867d\u7136<code>Math.random()<\/code>\u4e0d\u9002\u7528\u4e8e\u6240\u6709\u5b89\u5168\u76f8\u5173\u7684\u5e94\u7528\uff0c\u4f46\u662f\u5b83\u5bf9\u4e8e\u5927\u591a\u6570\u9700\u8981\u968f\u673a\u6027\u7684\u524d\u7aef\u5e94\u7528\u6765\u8bf4\u5df2\u7ecf\u8db3\u591f\u597d\u4e86\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>Math.floor()<\/code>\u7ed3\u5408<code>Math.random()<\/code><\/h3>\n<\/p>\n<p><p>\u7ed3\u5408<code>Math.random()<\/code>\u548c<code>Math.floor()<\/code>\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u6574\u6570\u3002<code>Math.floor(Math.random() * N)<\/code>\u5c06\u4ea7\u751f\u4e00\u4e2a\u4ece0\u5230N &#8211; 1\u4e4b\u95f4\u7684\u6574\u6570\u3002\u8fd9\u79cd\u65b9\u5f0f\u9002\u7528\u4e8e\u751f\u6210\u6570\u7ec4\u7d22\u5f15\u7b49\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u521b\u5efa\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u7684\u968f\u673a\u6574\u6570<\/h3>\n<\/p>\n<p><p>\u60f3\u8981\u521b\u5efa\u4e00\u4e2a\u4ecb\u4e8e\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u6309\u5982\u4e0b\u6b65\u9aa4\u64cd\u4f5c\uff1a<strong><code>Math.floor(Math.random() * (\u6700\u5927\u503c - \u6700\u5c0f\u503c + 1)) + \u6700\u5c0f\u503c<\/code><\/strong>\u3002\u8fd9\u79cd\u65b9\u6cd5\u901a\u8fc7\u6269\u5c55<code>Math.random()<\/code>\u8fd4\u56de\u7684\u8303\u56f4\uff0c\u518d\u901a\u8fc7\u6700\u5c0f\u503c\u8fdb\u884c\u504f\u79fb\uff0c\u53ef\u4ee5\u786e\u4fdd\u751f\u6210\u7684\u6574\u6570\u8986\u76d6\u6211\u4eec\u9700\u8981\u7684\u6240\u6709\u53ef\u80fd\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u8fdb\u9636\u4f7f\u7528 &#8211; \u5c01\u88c5\u4e3a\u51fd\u6570<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u5c06\u4e0a\u8ff0\u7684\u903b\u8f91\u5c01\u88c5\u4e3a\u4e00\u4e2a\u51fd\u6570\uff0c\u4fbf\u4e8e\u591a\u6b21\u8c03\u7528\u3002\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>getRandomInt(min, max)<\/code>\u7684\u51fd\u6570\uff0c\u5b83\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff1a\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\uff0c\u5e76\u8fd4\u56de\u4ecb\u4e8e\u8fd9\u4e24\u4e2a\u503c\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\u3002\u8fd9\u6837\uff0c\u6bcf\u5f53\u4f60\u9700\u8981\u4e00\u4e2a\u968f\u673a\u6574\u6570\uff0c\u53ea\u9700\u8981\u7b80\u5355\u5730\u8c03\u7528\u8fd9\u4e2a\u65b9\u6cd5\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u8fb9\u754c\u60c5\u51b5\u548c\u9519\u8bef\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u73b0\u4e0a\u8ff0\u51fd\u6570\u7684\u65f6\u5019\uff0c\u8fd8\u9700\u8003\u8651\u5230\u4e00\u4e9b\u8fb9\u754c\u60c5\u51b5\u3002\u6bd4\u5982\u6240\u63d0\u4f9b\u7684\u6700\u5927\u503c\u5c0f\u4e8e\u6700\u5c0f\u503c\uff0c\u6216\u8005\u8f93\u5165\u6570\u636e\u7c7b\u578b\u4e0d\u6b63\u786e\uff08\u4e0d\u662f\u6570\u5b57\u7c7b\u578b\uff09\u7b49\u3002\u8fd9\u5c31\u9700\u8981\u8fdb\u884c\u9519\u8bef\u5904\u7406\uff0c\u786e\u4fdd\u4ee3\u7801\u7684\u9c81\u68d2\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u968f\u673a\u6574\u6570\u5728\u524d\u7aef\u5e94\u7528\u4e2d\u7684\u4f7f\u7528\u6848\u4f8b<\/h3>\n<\/p>\n<p><p>\u751f\u6210\u968f\u673a\u6574\u6570\u5728\u524d\u7aef\u5f00\u53d1\u4e2d\u6709\u591a\u79cd\u7528\u9014\uff0c\u4f8b\u5982\u968f\u673a\u9009\u62e9\u6570\u7ec4\u5143\u7d20\u3001\u751f\u6210\u9a8c\u8bc1\u7801\u3001\u5b9e\u73b0\u8f6e\u64ad\u56fe\u7684\u968f\u673a\u64ad\u653e\u7b49\u3002\u8fd9\u4e9b\u573a\u666f\u91cc\uff0c\u5408\u7406\u5e76\u6709\u76ee\u7684\u5730\u4f7f\u7528\u968f\u673a\u6574\u6570\u80fd\u8ba9\u7528\u6237\u4f53\u9a8c\u66f4\u52a0\u751f\u52a8\u548c\u6709\u8da3\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5b89\u5168\u6027\u548c\u968f\u673a\u6570\u751f\u6210\u5668<\/h3>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u503c\u5f97\u6307\u51fa\u7684\u662f\uff0c\u5f53\u7528\u4e8e\u5b89\u5168\u76f8\u5173\u7684\u9886\u57df\u65f6\uff0c\u6bd4\u5982\u52a0\u5bc6\u5bc6\u94a5\u751f\u6210\u6216\u8005\u67d0\u4e9b\u6e38\u620f\u903b\u8f91\u7684\u5b9e\u73b0\uff0c<code>Math.random()<\/code>\u5c31\u53ef\u80fd\u4e0d\u591f\u5b89\u5168\u3002\u8fd9\u65f6\u5019\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u66f4\u4e13\u4e1a\u7684\u968f\u673a\u6570\u751f\u6210\u5668\uff0c\u5982Web Crypto API\u63d0\u4f9b\u7684<code>crypto.getRandomValues()<\/code>\u7b49\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u8ba8\u8bba\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5728\u524d\u7aefJavaScript\u4ee3\u7801\u4e2d\u521b\u5efa\u968f\u673a\u6574\u6570\u65e2\u7b80\u5355\u53c8\u7075\u6d3b\u3002\u65e0\u8bba\u662f\u57fa\u7840\u7684\u968f\u673a\u6574\u6570\u751f\u6210\u8fd8\u662f\u66f4\u9ad8\u7ea7\u7684\u4f7f\u7528\u573a\u666f\uff0cJavaScript\u90fd\u63d0\u4f9b\u4e86\u5fc5\u8981\u7684\u5de5\u5177\u6765\u6ee1\u8db3\u5f00\u53d1\u8005\u7684\u9700\u6c42\u3002\u53ea\u8981\u4e86\u89e3\u4e86<code>Math.random()<\/code>\u4e0e<code>Math.floor()<\/code>\u8fd9\u4e24\u4e2a\u65b9\u6cd5\u7684\u642d\u914d\u4f7f\u7528\uff0c\u5c31\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u968f\u673a\u6574\u578b\u6570\u503c\u7684\u751f\u6210\uff0c\u5e76\u53ef\u4ee5\u8fdb\u4e00\u6b65\u5730\u5c01\u88c5\u548c\u4f18\u5316\uff0c\u4f7f\u5176\u66f4\u9002\u5e94\u6211\u4eec\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>Q: \u5982\u4f55\u4f7f\u7528 JavaScript \u751f\u6210\u968f\u673a\u6574\u6570\uff1f<\/strong><\/p>\n<p>A: \u751f\u6210\u968f\u673a\u6574\u6570\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u3002\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528<code>Math.random()<\/code>\u51fd\u6570\u7ed3\u5408\u53d6\u6574\u64cd\u4f5c\u8fdb\u884c\u5904\u7406\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\u751f\u62101\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff1a<\/p>\n<pre><code class=\"language-javascript\">var randomInt = Math.floor(Math.random() * 10) + 1;\n<\/code><\/pre>\n<p>\u8fd9\u91cc<code>Math.random()<\/code>\u51fd\u6570\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u5c0f\u6570\uff0c\u7136\u540e\u4e58\u4ee510\u5f97\u5230\u4e00\u4e2a0\u523010\u4e4b\u95f4\u7684\u968f\u673a\u5c0f\u6570\uff0c\u518d\u4f7f\u7528<code>Math.floor()<\/code>\u51fd\u6570\u5411\u4e0b\u53d6\u6574\u5f97\u5230\u4e00\u4e2a0\u52309\u4e4b\u95f4\u7684\u6574\u6570\u3002\u6700\u540e\uff0c\u518d\u52a0\u4e0a1\uff0c\u5c06\u8303\u56f4\u53d8\u62101\u523010\u4e4b\u95f4\u7684\u6574\u6570\u3002<\/p>\n<p><strong>Q: JavaScript\u4e2d\u5982\u4f55\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff1f<\/strong><\/p>\n<p>A: \u8981\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u4e0a\u9762\u63d0\u5230\u7684\u65b9\u6cd5\u3002\u552f\u4e00\u7684\u533a\u522b\u662f\u9700\u8981\u8c03\u6574\u53d6\u6574\u64cd\u4f5c\u7684\u8303\u56f4\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u751f\u6210\u4e00\u4e2a1\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">var randomInt = Math.floor(Math.random() * 100) + 1;\n<\/code><\/pre>\n<p>\u8fd9\u91cc<code>Math.random()<\/code>\u751f\u62100\u52301\u4e4b\u95f4\u7684\u5c0f\u6570\uff0c\u4e58\u4ee5100\u5f97\u52300\u5230100\u4e4b\u95f4\u7684\u5c0f\u6570\uff0c\u518d\u52a0\u4e0a1\u5f97\u52301\u5230101\u4e4b\u95f4\u7684\u5c0f\u6570\uff08\u5305\u62ec1\u548c100\uff09\u3002\u6700\u540e\uff0c\u4f7f\u7528<code>Math.floor()<\/code>\u8fdb\u884c\u53d6\u6574\u64cd\u4f5c\u5f97\u52301\u5230100\u4e4b\u95f4\u7684\u6574\u6570\u3002<\/p>\n<p><strong>Q: \u5982\u4f55\u5728 JavaScript \u4e2d\u751f\u6210\u4e00\u4e2a\u53ef\u4ee5\u6307\u5b9a\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u7684\u968f\u673a\u6574\u6570\uff1f<\/strong><\/p>\n<p>A: \u8981\u751f\u6210\u4e00\u4e2a\u6307\u5b9a\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u7684\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u7c7b\u4f3c\u524d\u9762\u7684\u65b9\u6cd5\uff0c\u53ea\u9700\u8981\u8c03\u6574\u53d6\u6574\u64cd\u4f5c\u7684\u8303\u56f4\u548c\u504f\u79fb\u91cf\u5373\u53ef\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u751f\u6210\u4e00\u4e2a10\u523020\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-javascript\">var min = 10;\nvar max = 20;\nvar range = max - min + 1; \/\/ \u83b7\u53d6\u53d6\u503c\u8303\u56f4\nvar randomInt = Math.floor(Math.random() * range) + min;\n<\/code><\/pre>\n<p>\u8fd9\u91cc\u9996\u5148\u8ba1\u7b97\u51fa\u53d6\u503c\u8303\u56f4(<code>range<\/code>)\uff0c\u7136\u540e\u901a\u8fc7\u4e58\u4ee5<code>range<\/code>\u5f97\u52300\u5230<code>range - 1<\/code>\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u518d\u52a0\u4e0a\u6700\u5c0f\u503c(<code>min<\/code>)\u5f97\u5230\u6700\u7ec8\u7684\u968f\u673a\u6574\u6570\u3002\u8fd9\u6837\u5c31\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u6700\u5c0f\u503c\u548c\u6700\u5927\u503c\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5728\u524d\u7aefJavaScript\u4e2d\u521b\u5efa\u968f\u673a\u6574\u6570\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528 Math.random() \u65b9\u6cd5\u7ed3\u5408 Math.flo [&hellip;]","protected":false},"author":3,"featured_media":265393,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/265384"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=265384"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/265384\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/265393"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=265384"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=265384"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=265384"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}