{"id":159841,"date":"2024-05-08T13:18:07","date_gmt":"2024-05-08T05:18:07","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/159841.html"},"modified":"2024-05-08T13:18:09","modified_gmt":"2024-05-08T05:18:09","slug":"%e5%89%8d%e7%ab%af-javascript-%e5%a6%82%e4%bd%95%e5%ae%9e%e7%8e%b0%e4%bc%aa%e9%9a%8f%e6%9c%ba%e6%ad%a3%e6%80%81%e5%88%86%e5%b8%83","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/159841.html","title":{"rendered":"\u524d\u7aef JavaScript \u5982\u4f55\u5b9e\u73b0\u4f2a\u968f\u673a\u6b63\u6001\u5206\u5e03"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26222404\/cefaaab9-3f91-4b30-b94c-15f953c15519.webp\" alt=\"\u524d\u7aef JavaScript \u5982\u4f55\u5b9e\u73b0\u4f2a\u968f\u673a\u6b63\u6001\u5206\u5e03\" \/><\/p>\n<p><p>\u5728JavaScript\u4e2d\u5b9e\u73b0\u4f2a\u968f\u673a\u6b63\u6001\u5206\u5e03\u7684\u65b9\u6cd5\u4e3b\u8981\u4f9d\u8d56\u4e8e\u6570\u5b66\u6a21\u578b\u548c\u7b97\u6cd5\uff0c<strong>\u5305\u62ecBox-Muller\u53d8\u6362\u3001Ziggurat\u7b97\u6cd5\u3001\u4e2d\u5fc3\u6781\u9650\u5b9a\u7406<\/strong>\u7b49\u3002\u8fd9\u4e9b\u65b9\u6cd5\u53ef\u4ee5\u751f\u6210\u5177\u6709\u6b63\u6001\u5206\u5e03\u7279\u6027\u7684\u968f\u673a\u6570\u3002\u5176\u4e2d\uff0c<strong>Box-Muller\u53d8\u6362\u662f\u6700\u5e38\u7528\u4e14\u5b9e\u73b0\u7b80\u5355\u7684\u4e00\u79cd\u65b9\u6cd5<\/strong>\uff0c\u5b83\u901a\u8fc7\u5c06\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570\u8f6c\u6362\u4e3a\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u6765\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><p>Box-Muller\u53d8\u6362\u80cc\u540e\u7684\u601d\u60f3\u662f\u4ece\u72ec\u7acb\u540c\u5206\u5e03\uff08IID\uff09\u7684\u5747\u5300\u968f\u673a\u53d8\u91cf\u51fa\u53d1\uff0c\u901a\u8fc7\u6570\u5b66\u53d8\u6362\u751f\u6210\u5177\u6709\u6b63\u6001\u5206\u5e03\u7279\u6027\u7684\u968f\u673a\u53d8\u91cf\u3002\u8fd9\u79cd\u8f6c\u6362\u7684\u4f18\u70b9\u662f\u7b80\u5355\u4e14\u6548\u7387\u9ad8\uff0c\u9002\u5408\u4e8e\u5927\u591a\u6570\u9700\u8981\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e00\u3001BOX-MULLER\u53d8\u6362<\/strong><\/h3>\n<\/p>\n<p><p>Box-Muller\u53d8\u6362\u662f\u5b9e\u73b0\u4f2a\u968f\u673a\u6b63\u6001\u5206\u5e03\u7684\u4e00\u79cd\u6709\u6548\u6280\u672f\u3002\u5b83\u80fd\u591f\u4ece\u4e24\u4e2a\u72ec\u7acb\u7684\u5747\u5300\u5206\u5e03\u968f\u673a\u6570\u751f\u6210\u4e24\u4e2a\u72ec\u7acb\u7684\u6807\u51c6\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570\u3002\u53d8\u6362\u7684\u8fc7\u7a0b\u76f8\u5bf9\u7b80\u5355\uff0c\u9996\u5148\u751f\u6210\u4e24\u4e2a\uff080,1\uff09\u4e4b\u95f4\u7684\u72ec\u7acb\u5747\u5300\u5206\u5e03\u968f\u673a\u6570U1\u548cU2\uff0c\u7136\u540e\u901a\u8fc7\u4ee5\u4e0b\u516c\u5f0f\u8f6c\u6362\u4e3a\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570Z0\u548cZ1\u3002<\/p>\n<\/p>\n<p><p>[ Z_0 = \\sqrt{-2 \\log U_1} \\cos(2\\pi U_2) ]<\/p>\n<p>[ Z_1 = \\sqrt{-2 \\log U_1} \\sin(2\\pi U_2) ]<\/p>\n<\/p>\n<p><p>\u8fd9\u4e24\u4e2a\u503c\u5373\u9075\u5faa\u6807\u51c6\u6b63\u6001\u5206\u5e03\uff08\u5747\u503c\u4e3a0\uff0c\u65b9\u5dee\u4e3a1\uff09\u3002\u53ef\u4ee5\u901a\u8fc7\u8c03\u6574\u8fd9\u4e9b\u503c\u6765\u751f\u6210\u5177\u6709\u7279\u5b9a\u5747\u503c\u548c\u65b9\u5dee\u7684\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u8ba1\u7b97\u6548\u7387\u9ad8\uff0c\u5e76\u4e14\u751f\u6210\u7684\u968f\u673a\u6570\u5177\u6709\u826f\u597d\u7684\u7edf\u8ba1\u6027\u8d28\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e8c\u3001ZIGGURAT\u7b97\u6cd5<\/strong><\/h3>\n<\/p>\n<p><p>\u53e6\u4e00\u79cd\u5e38\u7528\u7684\u65b9\u6cd5\u662fZiggurat\u7b97\u6cd5\uff0c\u5b83\u662f\u4e00\u4e2a\u9ad8\u6548\u7684\u968f\u673a\u6b63\u6001\u5206\u5e03\u6570\u751f\u6210\u7b97\u6cd5\u3002\u8fd9\u79cd\u7b97\u6cd5\u7684\u6838\u5fc3\u601d\u60f3\u662f\u5c06\u6b63\u6001\u5206\u5e03\u66f2\u7ebf\u4e0b\u7684\u9762\u79ef\u5212\u5206\u4e3a\u82e5\u5e72\u5c42&quot;\u9636\u68af&quot;\uff0c\u6bcf\u4e00\u5c42\u90fd\u5bf9\u5e94\u4e00\u4e2a\u7279\u5b9a\u7684\u6982\u7387\u503c\u3002\u7136\u540e\uff0c\u7b97\u6cd5\u5728\u8fd9\u4e9b\u5c42\u95f4\u968f\u673a\u9009\u62e9\u4e00\u5c42\uff0c\u5e76\u5728\u8be5\u5c42\u5185\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><p>Ziggurat\u7b97\u6cd5\u7684\u4f18\u70b9\u662f\u901f\u5ea6\u5feb\uff0c\u5c24\u5176\u662f\u5f53\u9700\u8981\u5927\u91cf\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570\u65f6\u3002\u7136\u800c\uff0c\u5b83\u7684\u5b9e\u73b0\u76f8\u5bf9\u590d\u6742\uff0c\u9700\u8981\u9884\u5148\u8ba1\u7b97\u5e76\u5b58\u50a8\u7279\u5b9a\u7684\u9636\u68af\u5c42\u53c2\u6570\uff0c\u8fd9\u53ef\u4ee5\u901a\u8fc7\u79bb\u7ebf\u8ba1\u7b97\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e09\u3001\u4e2d\u5fc3\u6781\u9650\u5b9a\u7406<\/strong><\/h3>\n<\/p>\n<p><p>\u4e2d\u5fc3\u6781\u9650\u5b9a\u7406\u63d0\u4f9b\u4e86\u53e6\u4e00\u79cd\u751f\u6210\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570\u7684\u65b9\u6cd5\u3002\u8be5\u5b9a\u7406\u8868\u660e\uff0c\u591a\u4e2a\u72ec\u7acb\u540c\u5206\u5e03\u7684\u968f\u673a\u53d8\u91cf\u4e4b\u548c\u7684\u5206\u5e03\u968f\u7740\u53d8\u91cf\u6570\u91cf\u7684\u589e\u52a0\uff0c\u8d8b\u8fd1\u4e8e\u6b63\u6001\u5206\u5e03\u3002\u57fa\u4e8e\u8fd9\u4e00\u7406\u8bba\uff0c\u53ef\u4ee5\u901a\u8fc7\u7d2f\u52a0\u591a\u4e2a\u5747\u5300\u5206\u5e03\u968f\u673a\u6570\u6765\u8fd1\u4f3c\u751f\u6210\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4f8b\u5982\uff0c\u5c0612\u4e2a\uff080,1\uff09\u5747\u5300\u5206\u5e03\u968f\u673a\u6570\u76f8\u52a0\u5e76\u51cf\u53bb6\uff0c\u6240\u5f97\u7ed3\u679c\u5c06\u8fd1\u4f3c\u9075\u5faa\u6807\u51c6\u6b63\u6001\u5206\u5e03\u3002\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4f18\u70b9\u662f\u5b9e\u73b0\u7b80\u5355\uff0c\u4f46\u7cbe\u5ea6\u8f83\u4f4e\uff0c\u7279\u522b\u662f\u5728\u5206\u5e03\u7684\u5c3e\u90e8\u53ef\u80fd\u4e0d\u591f\u51c6\u786e\u3002<\/p>\n<\/p>\n<p><h3><strong>\u56db\u3001\u5b9e\u73b0\u793a\u4f8b\uff08\u4ee5BOX-MULLER\u53d8\u6362\u4e3a\u4f8b\uff09<\/strong><\/h3>\n<\/p>\n<p><p>\u5b9e\u73b0Box-Muller\u53d8\u6362\u7684JavaScript\u4ee3\u7801\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-javascript\">function boxMullerRandom() {<\/p>\n<p>  let u1 = 0, u2 = 0;<\/p>\n<p>  \/\/ \u5faa\u73af\u76f4\u5230\u975e\u96f6\u503c\u51fa\u73b0\u907f\u514dlog(0)\u7684\u60c5\u5f62<\/p>\n<p>  while(u1 === 0) u1 = Math.random();<\/p>\n<p>  while(u2 === 0) u2 = Math.random();<\/p>\n<p>  const R = Math.sqrt(-2.0 * Math.log(u1));<\/p>\n<p>  const theta = 2.0 * Math.PI * u2;<\/p>\n<p>  const Z0 = R * Math.cos(theta);<\/p>\n<p>  \/\/ const Z1 = R * Math.sin(theta); \/\/ \u7b2c\u4e8c\u4e2a\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570\uff08\u53ef\u9009\u4f7f\u7528\uff09<\/p>\n<p>  return Z0;<\/p>\n<p>}<\/p>\n<p>\/\/ \u751f\u6210\u5177\u6709\u7279\u5b9a\u5747\u503c(mu)\u548c\u65b9\u5dee(sigma^2)\u7684\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570<\/p>\n<p>function generateNormal(mu = 0, sigma = 1) {<\/p>\n<p>  const Z0 = boxMullerRandom();<\/p>\n<p>  return Z0 * sigma + mu;<\/p>\n<p>}<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8be5\u4ee3\u7801\u4e2d\uff0c<code>boxMullerRandom<\/code>\u51fd\u6570\u5b9e\u73b0\u4e86Box-Muller\u53d8\u6362\uff0c\u751f\u6210\u6807\u51c6\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u3002<code>generateNormal<\/code>\u51fd\u6570\u5219\u5141\u8bb8\u751f\u6210\u5177\u6709\u4efb\u610f\u5747\u503c\u548c\u65b9\u5dee\u7684\u6b63\u6001\u5206\u5e03\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><h3><strong>\u4e94\u3001\u7ed3\u8bba<\/strong><\/h3>\n<\/p>\n<p><p>\u5728\u524d\u7aefJavaScript\u73af\u5883\u4e2d\u5b9e\u73b0\u4f2a\u968f\u673a\u6b63\u6001\u5206\u5e03\uff0c\u53ef\u4ee5\u91c7\u7528\u591a\u79cd\u6570\u5b66\u6a21\u578b\u548c\u7b97\u6cd5\u3002Box-Muller\u53d8\u6362\u56e0\u5176\u7b80\u5355\u6027\u548c\u9ad8\u6548\u6027\u6210\u4e3a\u5e38\u7528\u65b9\u6cd5\uff0c\u800cZiggurat\u7b97\u6cd5\u5728\u9700\u8981\u9ad8\u6548\u751f\u6210\u5927\u91cf\u968f\u673a\u6570\u65f6\u8868\u73b0\u4f18\u5f02\u3002\u4e2d\u5fc3\u6781\u9650\u5b9a\u7406\u5219\u63d0\u4f9b\u4e86\u4e00\u79cd\u601d\u8def\u7b80\u5355\u4f46\u7cbe\u5ea6\u8f83\u4f4e\u7684\u66ff\u4ee3\u65b9\u6cd5\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u548c\u6548\u7387\u8003\u8651\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u4ec0\u4e48\u662f\u4f2a\u968f\u673a\u6b63\u6001\u5206\u5e03\uff1f<\/strong><\/p>\n<p>\u4f2a\u968f\u673a\u6b63\u6001\u5206\u5e03\u662f\u4e00\u79cd\u8fd1\u4f3c\u4e8e\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u751f\u6210\u65b9\u6cd5\u3002\u5728\u524d\u7aef JavaScript \u4e2d\uff0c\u6211\u4eec\u53ef\u4ee5\u5229\u7528\u4e00\u4e9b\u6570\u5b66\u7b97\u6cd5\u6765\u751f\u6210\u63a5\u8fd1\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u5728\u524d\u7aef JavaScript \u4e2d\u751f\u6210\u4f2a\u968f\u673a\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\uff1f<\/strong><\/p>\n<p>\u751f\u6210\u4f2a\u968f\u673a\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u7684\u4e00\u4e2a\u5e38\u7528\u65b9\u6cd5\u662f\u4f7f\u7528\u4e2d\u5fc3\u6781\u9650\u5b9a\u7406\uff0c\u7ed3\u5408\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570\u751f\u6210\u5668\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u5747\u5300\u5206\u5e03\u7684\u968f\u673a\u6570\u751f\u6210\u5668\u751f\u6210\u4e24\u4e2a\u5728 [0, 1) \u533a\u95f4\u5185\u7684\u968f\u673a\u6570 U1 \u548c U2\u3002<\/li>\n<li>\u8ba1\u7b97 Z1 = \u221a(-2 * ln(U1)) * cos(2 * \u03c0 * U2) \u548c Z2 = \u221a(-2 * ln(U1)) * sin(2 * \u03c0 * U2)\u3002<\/li>\n<li>Z1 \u548c Z2 \u90fd\u662f\u8fd1\u4f3c\u4e8e\u6807\u51c6\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u3002<\/li>\n<\/ul>\n<p><strong>3. \u5728\u524d\u7aef JavaScript \u4e2d\u5982\u4f55\u8c03\u6574\u4f2a\u968f\u673a\u6b63\u6001\u5206\u5e03\u7684\u53c2\u6570\uff1f<\/strong><\/p>\n<p>\u5982\u679c\u9700\u8981\u8c03\u6574\u4f2a\u968f\u673a\u6b63\u6001\u5206\u5e03\u7684\u53c2\u6570\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u6cd5\u5b9e\u73b0\uff1a<\/p>\n<ul>\n<li>\u8c03\u6574\u5747\u503c\uff1a\u53ef\u4ee5\u901a\u8fc7\u5728\u751f\u6210\u7684\u968f\u673a\u6570\u4e0a\u52a0\u4e0a\u6307\u5b9a\u7684\u5747\u503c\u6765\u8fbe\u5230\u8c03\u6574\u5747\u503c\u7684\u76ee\u7684\u3002<\/li>\n<li>\u8c03\u6574\u6807\u51c6\u5dee\uff1a\u53ef\u4ee5\u901a\u8fc7\u5728\u751f\u6210\u7684\u968f\u673a\u6570\u4e0a\u4e58\u4ee5\u6307\u5b9a\u7684\u6807\u51c6\u5dee\u6765\u8fbe\u5230\u8c03\u6574\u6807\u51c6\u5dee\u7684\u76ee\u7684\u3002<\/li>\n<\/ul>\n<p>\u6839\u636e\u9700\u8981\uff0c\u6211\u4eec\u53ef\u4ee5\u81ea\u5b9a\u4e49\u5b9e\u73b0\u76f8\u5e94\u7684\u51fd\u6570\u6765\u751f\u6210\u7b26\u5408\u7279\u5b9a\u53c2\u6570\u7684\u4f2a\u968f\u673a\u6b63\u6001\u5206\u5e03\u7684\u968f\u673a\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728JavaScript\u4e2d\u5b9e\u73b0\u4f2a\u968f\u673a\u6b63\u6001\u5206\u5e03\u7684\u65b9\u6cd5\u4e3b\u8981\u4f9d\u8d56\u4e8e\u6570\u5b66\u6a21\u578b\u548c\u7b97\u6cd5\uff0c\u5305\u62ecBox-Muller\u53d8\u6362\u3001Zig [&hellip;]","protected":false},"author":3,"featured_media":159842,"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\/159841"}],"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=159841"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/159841\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/159842"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=159841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=159841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=159841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}