{"id":1361,"date":"2015-12-14T12:49:11","date_gmt":"2015-12-14T04:49:11","guid":{"rendered":"http:\/\/www.gaigaiming.com\/?p=1361"},"modified":"2015-12-14T12:49:11","modified_gmt":"2015-12-14T04:49:11","slug":"%e9%9a%8f%e6%9c%ba%e6%95%b0math-random","status":"publish","type":"post","link":"https:\/\/www.gaigaiming.com\/index.php\/2015\/12\/14\/%e9%9a%8f%e6%9c%ba%e6%95%b0math-random\/","title":{"rendered":"\u968f\u673a\u6570Math.random()"},"content":{"rendered":"<p><script src=\"http:\/\/assets.changyan.sohu.com\/upload\/changyan.js?conf=prod_1dc5dc80970106099d43fe242b7465fc&amp;appid=cyrHH3dwi\" type=\"text\/javascript\" charset=\"utf-8\"><\/script><script src=\"\/\/hm.baidu.com\/hm.js?b88cfc1ccab788f0903cac38c894caa3\"><\/script><script src=\"http:\/\/pagead2.googlesyndication.com\/pub-config\/ca-pub-6384567588307613.js\"><\/script><\/p>\n<div id=\"head\">\n<div id=\"contents\">\n<div class=\"main clearfix\">\n<div class=\"w690 fl\">\n<div class=\"article-content border\">\n<div id=\"content\">\n<p>\u672c\u6587\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528js\u751f\u6210n\u5230m\u95f4\u7684\u968f\u673a\u6570\u5b57\uff0c\u4e3b\u8981\u76ee\u7684\u662f\u4e3a\u540e\u671f\u7684js\u751f\u6210\u9a8c\u8bc1\u7801\u505a\u51c6\u5907\u3002<\/p>\n<p>Math.random()\u51fd\u6570\u8fd4\u56de0\u548c1\u4e4b\u95f4\u7684\u4f2a\u968f\u673a\u6570\uff0c<strong>\u53ef\u80fd\u4e3a0<\/strong>\uff0c\u4f46\u603b\u662f<strong>\u5c0f\u4e8e1<\/strong>\uff0c[0,1)<\/p>\n<p>\u751f\u6210n-m\uff0c\u5305\u542bn\u4f46\u4e0d\u5305\u542bm\u7684\u6574\u6570\uff1a<\/p>\n<p>\u7b2c\u4e00\u6b65\u7b97\u51fa m-n\u7684\u503c\uff0c\u5047\u8bbe\u7b49\u4e8ew<\/p>\n<p>\u7b2c\u4e8c\u6b65Math.random()*w<\/p>\n<p>\u7b2c\u4e09\u6b65Math.random()*w+n<\/p>\n<p>\u7b2c\u56db\u6b65parseInt(Math.random()*w+n, 10)<\/p>\n<p>\u751f\u6210n-m\uff0c\u4e0d\u5305\u542bn\u4f46\u5305\u542bm\u7684\u6574\u6570\uff1a\u200b<\/p>\n<p>\u7b2c\u4e00\u6b65\u7b97\u51fa m-n\u7684\u503c\uff0c\u5047\u8bbe\u7b49\u4e8ew<\/p>\n<p>\u7b2c\u4e8c\u6b65Math.random()*w<\/p>\n<p>\u7b2c\u4e09\u6b65Math.random()*w+n<\/p>\n<p>\u7b2c\u56db\u6b65Math.floor(Math.random()*w+n) + 1<\/p>\n<p>\u751f\u6210n-m\uff0c\u4e0d\u5305\u542bn\u548cm\u7684\u6574\u6570\uff1a<\/p>\n<p>\u7b2c\u4e00\u6b65\u7b97\u51fa m-n-2\u7684\u503c\uff0c\u5047\u8bbe\u7b49\u4e8ew<\/p>\n<p>\u7b2c\u4e8c\u6b65Math.random()*w<\/p>\n<p>\u7b2c\u4e09\u6b65Math.random()*w+n +1<\/p>\n<p>\u7b2c\u56db\u6b65Math.round(Math.random()*w+n+1) \u6216\u8005 Math.ceil(Math.random()*w+n+1)<\/p>\n<p>\u751f\u6210n-m\uff0c\u5305\u542bn\u548cm\u7684\u968f\u673a\u6570\uff1a<\/p>\n<p>\u7b2c\u4e00\u6b65\u7b97\u51fa m-n\u7684\u503c\uff0c\u5047\u8bbe\u7b49\u4e8ew<\/p>\n<p>\u7b2c\u4e8c\u6b65Math.random()*w<\/p>\n<p>\u7b2c\u4e09\u6b65Math.random()*w+n<\/p>\n<p>\u7b2c\u56db\u6b65Math.round(Math.random()*w+n) \u6216\u8005 Math.ceil(Math.random()*w+n)<\/p>\n<p>\u4f8b\u5b50\uff1a<\/p>\n<p>\u751f\u6210800-1500\u7684\u968f\u673a\u6574\u6570\uff0c\u5305\u542b800\u4f46\u4e0d\u5305\u542b1500<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b:<\/p>\n<div id=\"code29545\" class=\"codebody\">1500-800 = 700<br \/>\nMath.random()*700<br \/>\nvar num = Math.random()*700 + 800;<br \/>\nnum = parseInt(num, 10);<\/div>\n<p>\u53ea\u9700\u8981\u7b80\u5355\u7684\u56db\u6b65\u5c31\u53ef\u4ee5\u5b8c\u6210\u3002<\/p>\n<p>\u8865\u5145\uff1a\u3000\u3000Math.ceil() \u8fd4\u56de\u5927\u4e8e\u7b49\u4e8e\u6570\u5b57\u53c2\u6570\u7684\u6700\u5c0f\u6574\u6570(\u53d6\u6574\u51fd\u6570)\uff0c\u5bf9\u6570\u5b57\u8fdb\u884c\u4e0a\u820d\u5165<\/p>\n<p>Math.floor() \u8fd4\u56de\u5c0f\u4e8e\u7b49\u4e8e\u6570\u5b57\u53c2\u6570\u7684\u6700\u5927\u6574\u6570\uff0c\u5bf9\u6570\u5b57\u8fdb\u884c\u4e0b\u820d\u5165<\/p>\n<p>Math.round() \u8fd4\u56de\u6570\u5b57\u6700\u63a5\u8fd1\u7684\u6574\u6570\uff0c\u56db\u820d\u4e94\u5165<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u8bb2\u89e3\u5982\u4f55\u4f7f\u7528js\u751f\u6210n\u5230m\u95f4\u7684\u968f\u673a\u6570\u5b57\uff0c\u4e3b\u8981\u76ee\u7684\u662f\u4e3a\u540e\u671f\u7684js\u751f\u6210\u9a8c\u8bc1\u7801\u505a\u51c6\u5907\u3002 Math.random( [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-1361","post","type-post","status-publish","format-standard","hentry","category-4"],"_links":{"self":[{"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/posts\/1361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/comments?post=1361"}],"version-history":[{"count":0,"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/posts\/1361\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/media?parent=1361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/categories?post=1361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gaigaiming.com\/index.php\/wp-json\/wp\/v2\/tags?post=1361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}