{"id":201897,"date":"2020-10-07T09:09:07","date_gmt":"2020-10-07T01:09:07","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=201897"},"modified":"2020-09-25T13:11:57","modified_gmt":"2020-09-25T05:11:57","slug":"js-can-use","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/js-can-use.html","title":{"rendered":"js\u53ef\u4ee5\u4f7f\u7528 Math\uff08\u7b97\u6570\uff09 \u5bf9\u8c61\u751f\u6210\u968f\u673a\u6570"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\njs \u53ef\u4ee5\u4f7f\u7528 Math\uff08\u7b97\u6570\uff09 \u5bf9\u8c61\u6765\u5b9e\u73b0\u968f\u673a\u6570\u7684\u751f\u6210\u3002<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\"\"<\/p>\n

\u9700\u8981\u4e86\u89e3\u7684 Math \u5bf9\u8c61\u65b9\u6cd5<\/strong><\/div>\n\n\n\n\n\n\n<\/tr>\n\n\n
\u65b9\u6cd5<\/th>\n\u63cf\u8ff0<\/th>\n<\/tr>\n
ceil(x)<\/a><\/td>\n\u5bf9\u6570\u8fdb\u884c\u4e0a\u820d\u5165\uff0c\u5373\u5411\u4e0a\u53d6\u6574\u3002<\/td>\n<\/tr>\n
floor(x)<\/a><\/td>\n\u5bf9 x \u8fdb\u884c\u4e0b\u820d\u5165\uff0c\u5373\u5411\u4e0b\u53d6\u6574\u3002<\/td>\n<\/tr>\n
round(x)<\/a><\/td>\n\u56db\u820d\u4e94\u5165\u3002<\/td>\n<\/tr>\n
random()<\/a><\/td>\n\u8fd4\u56de 0 ~ 1 \u4e4b\u95f4\u7684\u968f\u673a\u6570\uff0c\u5305\u542b 0 \u4e0d\u5305\u542b 1\u3002<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u4e00\u4e9b\u5b9e\u4f8b\u8bf4\u660e\uff1a<\/p>\n

Math.ceil(Math.random()*10);     \/\/ \u83b7\u53d6\u4ece 1 \u5230 10 \u7684\u968f\u673a\u6574\u6570\uff0c\u53d6 0 \u7684\u6982\u7387\u6781\u5c0f\u3002\r\n\r\nMath.round(Math.random());       \/\/ \u53ef\u5747\u8861\u83b7\u53d6 0 \u5230 1 \u7684\u968f\u673a\u6574\u6570\u3002\r\n\r\nMath.floor(Math.random()*10);    \/\/ \u53ef\u5747\u8861\u83b7\u53d6 0 \u5230 9 \u7684\u968f\u673a\u6574\u6570\u3002\r\n\r\nMath.round(Math.random()*10);    \/\/ \u57fa\u672c\u5747\u8861\u83b7\u53d6 0 \u5230 10 \u7684\u968f\u673a\u6574\u6570\uff0c\u5176\u4e2d\u83b7\u53d6\u6700\u5c0f\u503c 0 \u548c\u6700\u5927\u503c 10 \u7684\u51e0\u7387\u5c11\u4e00\u534a\u3002<\/pre>\n

\u56e0\u4e3a\u7ed3\u679c\u5728 0~0.4 \u4e3a 0\uff0c0.5 \u5230 1.4 \u4e3a 1\uff0c8.5 \u5230 9.4 \u4e3a 9\uff0c9.5 \u5230 9.9 \u4e3a 10\u3002\u6240\u4ee5\u5934\u5c3e\u7684\u5206\u5e03\u533a\u95f4\u53ea\u6709\u5176\u4ed6\u6570\u5b57\u7684\u4e00\u534a\u3002<\/p>\n

\u751f\u6210 [n,m] \u7684\u968f\u673a\u6574\u6570<\/strong><\/div>\n

\u51fd\u6570\u529f\u80fd<\/strong>\uff1a\u751f\u6210 [n,m] \u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n

\u5728 js \u751f\u6210\u9a8c\u8bc1\u7801\u6216\u8005\u968f\u673a\u9009\u4e2d\u4e00\u4e2a\u9009\u9879\u65f6\u5f88\u6709\u7528\u3002<\/p>\n

\/\/\u751f\u6210\u4eceminNum\u5230maxNum\u7684\u968f\u673a\u6570\r\nfunction randomNum(minNum,maxNum){ \r\n    switch(arguments.length){ \r\n        case 1: \r\n            return parseInt(Math.random()*minNum+1,10); \r\n        break; \r\n        case 2: \r\n            return parseInt(Math.random()*(maxNum-minNum+1)+minNum,10); \r\n        break; \r\n            default: \r\n                return 0; \r\n            break; \r\n    } \r\n} <\/pre>\n

\u8fc7\u7a0b\u5206\u6790<\/strong>\uff1a<\/p>\n

Math.random() \u751f\u6210 [0,1) \u7684\u6570\uff0c\u6240\u4ee5 Math.random()*5 \u751f\u6210 {0,5) \u7684\u6570\u3002<\/p>\n

\u901a\u5e38\u671f\u671b\u5f97\u5230\u6574\u6570\uff0c\u6240\u4ee5\u8981\u5bf9\u5f97\u5230\u7684\u7ed3\u679c\u5904\u7406\u4e00\u4e0b\u3002<\/p>\n

parseInt()\uff0cMath.floor()\uff0cMath.ceil() \u548c Math.round() \u90fd\u53ef\u5f97\u5230\u6574\u6570\u3002<\/p>\n

parseInt() \u548c Math.floor() \u7ed3\u679c\u90fd\u662f\u5411\u4e0b\u53d6\u6574\u3002<\/p>\n

\u6240\u4ee5 Math.random()*5 \u751f\u6210\u7684\u90fd\u662f [0,4] \u7684\u968f\u673a\u6574\u6570\u3002<\/p>\n

\u6240\u4ee5\u751f\u6210 [1,max] \u7684\u968f\u673a\u6570\uff0c\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n

\/\/ max - \u671f\u671b\u7684\u6700\u5927\u503c\r\nparseInt(Math.random()*max,10)+1;\r\nMath.floor(Math.random()*max)+1;\r\nMath.ceil(Math.random()*max);<\/pre>\n

\u6240\u4ee5\u751f\u6210 [0,max] \u5230\u4efb\u610f\u6570\u7684\u968f\u673a\u6570\uff0c\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n

\/\/ max - \u671f\u671b\u7684\u6700\u5927\u503c\r\nparseInt(Math.random()*(max+1),10);\r\nMath.floor(Math.random()*(max+1));<\/pre>\n

\u6240\u4ee5\u5e0c\u671b\u751f\u6210 [min,max] \u7684\u968f\u673a\u6570\uff0c\u516c\u5f0f\u5982\u4e0b\uff1a<\/p>\n

\/\/ max - \u671f\u671b\u7684\u6700\u5927\u503c\r\n\/\/ min - \u671f\u671b\u7684\u6700\u5c0f\u503c\r\nparseInt(Math.random()*(max-min+1)+min,10);\r\nMath.floor(Math.random()*(max-min+1)+min);<\/pre>\n","protected":false},"excerpt":{"rendered":"

\u65b9\u6cd5 \u63cf\u8ff0 ceil(x) \u5bf9\u6570\u8fdb\u884c\u4e0a\u820d\u5165\uff0c\u5373\u5411\u4e0a\u53d6\u6574\u3002 floor(x) \u5bf9 x \u8fdb\u884c\u4e0b\u820d\u5165\uff0c\u5373\u5411\u4e0b\u53d6\u6574\u3002 […]<\/p>\n","protected":false},"author":321,"featured_media":201899,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-201897","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/201897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/321"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=201897"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/201897\/revisions"}],"predecessor-version":[{"id":201903,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/201897\/revisions\/201903"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/201899"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=201897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=201897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=201897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}