{"id":1560,"date":"2024-11-10T16:02:17","date_gmt":"2024-11-10T08:02:17","guid":{"rendered":"https:\/\/fwq.ai\/blog\/1560\/"},"modified":"2024-11-10T16:02:17","modified_gmt":"2024-11-10T08:02:17","slug":"javascript-%e4%b8%ad%e5%a6%82%e4%bd%95%e7%94%9f%e6%88%90%e9%9a%8f%e6%9c%ba%e6%95%b0%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/1560\/","title":{"rendered":"JavaScript \u4e2d\u5982\u4f55\u751f\u6210\u968f\u673a\u6570\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/173046701448328.jpg\" class=\"aligncenter\" title=\"JavaScript \u4e2d\u5982\u4f55\u751f\u6210\u968f\u673a\u6570\uff1f\u63d2\u56fe\" alt=\"JavaScript \u4e2d\u5982\u4f55\u751f\u6210\u968f\u673a\u6570\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong>javascript \u968f\u673a\u6570\u751f\u6210\u65b9\u6cd5\u4ecb\u7ecd\u53ca\u5199\u6cd5<\/strong><\/p>\n<p>\u5728 javascript \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u751f\u6210\u968f\u673a\u6570\u3002\u6700\u5e38\u7528\u4e5f\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 math.random() \u51fd\u6570\u3002<\/p>\n<p><strong>math.random() \u51fd\u6570<\/strong><\/p>\n<p>math.random() \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u4ecb\u4e8e 0\uff08\u5305\u62ec\uff09\u548c 1\uff08\u4e0d\u5305\u62ec\uff09\u4e4b\u95f4\u7684\u6d6e\u70b9\u6570\u3002\u4f7f\u7528\u8be5\u51fd\u6570\u751f\u6210\u968f\u673a\u6570\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre>const randomNumber = Math.random();<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u65b9\u6cd5<\/strong><\/p>\n<p><span>\u7acb\u5373\u5b66\u4e60<\/span>\u201c\u201d\uff1b<\/p>\n<p>\u9664\u4e86 math.random() \u51fd\u6570\u5916\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6 math \u51fd\u6570\u751f\u6210\u66f4\u590d\u6742\u7684\u968f\u673a\u6570\u3002<\/p>\n<ul>\n<li> <strong>math.round(math.random())<\/strong>\uff1a\u751f\u6210\u4e00\u4e2a\u968f\u673a\u7684 0 \u6216 1\u3002<\/li>\n<li> <strong>math.floor(math.random() * 10)<\/strong>\uff1a\u751f\u6210\u4e00\u4e2a\u968f\u673a\u7684 0 \u5230 9 \u4e4b\u95f4\u7684\u6574\u578b\u6570\u3002<\/li>\n<li> <strong>math.floor(math.random() * 10) % 3<\/strong>: \u751f\u6210\u4e00\u4e2a\u968f\u673a\u7684 0 \u5230 2 \u4e4b\u95f4\u7684\u6574\u578b\u6570\u3002<\/li>\n<li> <strong>math.round(math.random() * (max &#8211; min) + min)<\/strong>\uff1a\u751f\u6210\u4e00\u4e2a\u968f\u673a\u7684\u4ecb\u4e8e min \u548c max \u4e4b\u95f4\u7684\u6574\u578b\u6570\uff08\u5305\u542b min\uff0c\u4e0d\u5305\u542b max\uff09\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fJavaScript \u4e2d\u5982\u4f55\u751f\u6210\u968f\u673a\u6570\uff1f\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>javascript \u968f\u673a\u6570\u751f\u6210\u65b9\u6cd5\u4ecb\u7ecd\u53ca\u5199\u6cd5 \u5728 javascript \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u6cd5\u751f\u6210\u968f\u673a\u6570\u3002\u6700\u5e38\u7528\u4e5f\u662f\u6700\u7b80\u5355\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 math.random() \u51fd\u6570\u3002 math.random() \u51fd\u6570 math.random() \u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u4ecb\u4e8e 0\uff08\u5305\u62ec\uff09\u548c 1\uff08\u4e0d\u5305\u62ec\uff09\u4e4b\u95f4\u7684\u6d6e\u70b9\u6570\u3002\u4f7f\u7528\u8be5\u51fd\u6570\u751f\u6210\u968f\u673a\u6570\u7684\u4ee3\u7801\u5982\u4e0b\uff1a const randomNumber = Math.random(); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4ed6\u65b9\u6cd5 \u7acb\u5373\u5b66\u4e60\u201c\u201d\uff1b \u9664\u4e86 math.random() \u51fd\u6570\u5916\uff0c\u8fd8\u53ef\u4ee5\u7ed3\u5408\u5176\u4ed6 math \u51fd\u6570\u751f\u6210\u66f4\u590d\u6742\u7684\u968f\u673a\u6570\u3002 math.round(math.random())\uff1a\u751f\u6210\u4e00\u4e2a\u968f\u673a\u7684 0 \u6216 1\u3002 math.floor(math.random() * 10)\uff1a\u751f\u6210\u4e00\u4e2a\u968f\u673a\u7684 0 \u5230 9 \u4e4b\u95f4\u7684\u6574\u578b\u6570\u3002 math.floor(math.random() * 10) % 3: \u751f\u6210\u4e00\u4e2a\u968f\u673a\u7684 0 \u5230 2 \u4e4b\u95f4\u7684\u6574\u578b\u6570\u3002 math.round(math.random() * (max &#8211; min) + min)\uff1a\u751f\u6210\u4e00\u4e2a\u968f\u673a\u7684\u4ecb\u4e8e min \u548c [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-1560","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=1560"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/1560\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=1560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=1560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=1560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}