{"id":1104469,"date":"2025-01-08T16:20:55","date_gmt":"2025-01-08T08:20:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1104469.html"},"modified":"2025-01-08T16:21:15","modified_gmt":"2025-01-08T08:21:15","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8python%e4%b8%ad%e7%94%9f%e6%88%905%e4%bd%8d%e9%9a%8f%e6%9c%ba%e5%b0%8f%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1104469.html","title":{"rendered":"\u5982\u4f55\u5728python\u4e2d\u751f\u62105\u4f4d\u968f\u673a\u5c0f\u6570"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25065825\/5672e92c-e9da-40ba-91d4-a5f33c0df691.webp\" alt=\"\u5982\u4f55\u5728python\u4e2d\u751f\u62105\u4f4d\u968f\u673a\u5c0f\u6570\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\u751f\u62105\u4f4d\u968f\u673a\u5c0f\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u4f7f\u7528random\u6a21\u5757\u3001numpy\u5e93\u3001\u4ee5\u53ca\u4f7f\u7528decimal\u6a21\u5757\u3002<\/strong>\u5176\u4e2d\uff0c\u6700\u5e38\u7528\u7684\u65b9\u6cd5\u662f\u4f7f\u7528random\u6a21\u5757\u3002<strong>random\u6a21\u5757\u7684\u4f18\u70b9\u662f\u7b80\u5355\u6613\u7528\u3001\u76f4\u63a5\u751f\u6210\u968f\u673a\u6570\u3001\u7cbe\u786e\u63a7\u5236\u5c0f\u6570\u4f4d\u6570\u3002<\/strong>\u4e0b\u9762\u8be6\u7ec6\u4ecb\u7ecd\u6bcf\u79cd\u65b9\u6cd5\u53ca\u5176\u5177\u4f53\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528random\u6a21\u5757<\/p>\n<\/p>\n<p><p>random\u6a21\u5757\u662fPython\u5185\u7f6e\u7684\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\u7684\u6a21\u5757\u3002\u901a\u8fc7random\u6a21\u5757\u53ef\u4ee5\u751f\u6210\u5404\u79cd\u968f\u673a\u6570\uff0c\u5305\u62ec\u6574\u6570\u3001\u6d6e\u70b9\u6570\u7b49\u3002\u4e0b\u9762\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528random\u6a21\u5757\u751f\u62105\u4f4d\u968f\u673a\u5c0f\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>random_float = random.random()<\/p>\n<h2><strong>\u5c06\u968f\u673a\u6d6e\u70b9\u6570\u4e58\u4ee5\u4e00\u4e2a\u5408\u9002\u7684\u500d\u6570\uff0c\u4fdd\u7559\u4e94\u4f4d\u5c0f\u6570<\/strong><\/h2>\n<p>random_float_5_decimal = round(random_float * 10&lt;strong&gt;5) \/ 10&lt;\/strong&gt;5<\/p>\n<p>print(random_float_5_decimal)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u7136\u540e\u5c06\u8fd9\u4e2a\u6d6e\u70b9\u6570\u4e58\u4ee510\u76845\u6b21\u65b9\uff08\u5373100000\uff09\uff0c\u5e76\u4f7f\u7528round\u51fd\u6570\u4fdd\u7559\u4e94\u4f4d\u5c0f\u6570\u3002\u6700\u540e\u518d\u9664\u4ee510\u76845\u6b21\u65b9\uff0c\u4ee5\u5f97\u5230\u4e00\u4e2a5\u4f4d\u968f\u673a\u5c0f\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528numpy\u5e93<\/p>\n<\/p>\n<p><p>numpy\u662fPython\u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u63d0\u4f9b\u4e86\u8bb8\u591a\u9ad8\u7ea7\u7684\u6570\u5b66\u51fd\u6570\u548c\u968f\u673a\u6570\u751f\u6210\u51fd\u6570\u3002\u901a\u8fc7numpy\u5e93\u4e5f\u53ef\u4ee5\u751f\u62105\u4f4d\u968f\u673a\u5c0f\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>random_float = np.random.random()<\/p>\n<h2><strong>\u5c06\u968f\u673a\u6d6e\u70b9\u6570\u4e58\u4ee5\u4e00\u4e2a\u5408\u9002\u7684\u500d\u6570\uff0c\u4fdd\u7559\u4e94\u4f4d\u5c0f\u6570<\/strong><\/h2>\n<p>random_float_5_decimal = np.round(random_float * 10&lt;strong&gt;5) \/ 10&lt;\/strong&gt;5<\/p>\n<p>print(random_float_5_decimal)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u4f7f\u7528numpy\u7684random\u6a21\u5757\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u7136\u540e\u5c06\u8fd9\u4e2a\u6d6e\u70b9\u6570\u4e58\u4ee510\u76845\u6b21\u65b9\uff0c\u5e76\u4f7f\u7528numpy\u7684round\u51fd\u6570\u4fdd\u7559\u4e94\u4f4d\u5c0f\u6570\u3002\u6700\u540e\u518d\u9664\u4ee510\u76845\u6b21\u65b9\uff0c\u4ee5\u5f97\u5230\u4e00\u4e2a5\u4f4d\u968f\u673a\u5c0f\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528decimal\u6a21\u5757<\/p>\n<\/p>\n<p><p>decimal\u6a21\u5757\u662fPython\u4e2d\u7528\u4e8e\u9ad8\u7cbe\u5ea6\u6d6e\u70b9\u6570\u8ba1\u7b97\u7684\u6a21\u5757\u3002\u901a\u8fc7decimal\u6a21\u5757\u53ef\u4ee5\u751f\u6210\u9ad8\u7cbe\u5ea6\u7684\u968f\u673a\u5c0f\u6570\uff0c\u5e76\u63a7\u5236\u5176\u5c0f\u6570\u4f4d\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>from decimal import Decimal, getcontext<\/p>\n<h2><strong>\u8bbe\u7f6edecimal\u6a21\u5757\u7684\u7cbe\u5ea6<\/strong><\/h2>\n<p>getcontext().prec = 7<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>random_float = random.random()<\/p>\n<h2><strong>\u5c06\u968f\u673a\u6d6e\u70b9\u6570\u8f6c\u6362\u4e3adecimal\u7c7b\u578b\uff0c\u5e76\u4fdd\u7559\u4e94\u4f4d\u5c0f\u6570<\/strong><\/h2>\n<p>random_float_5_decimal = Decimal(random_float).quantize(Decimal(&#39;0.00000&#39;))<\/p>\n<p>print(random_float_5_decimal)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u8bbe\u7f6edecimal\u6a21\u5757\u7684\u7cbe\u5ea6\u4e3a7\u4f4d\uff0c\u4ee5\u4fbf\u540e\u7eed\u8ba1\u7b97\u4e2d\u4fdd\u7559\u8db3\u591f\u7684\u7cbe\u5ea6\u3002\u7136\u540e\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3adecimal\u7c7b\u578b\u3002\u6700\u540e\u4f7f\u7528quantize\u65b9\u6cd5\u4fdd\u7559\u4e94\u4f4d\u5c0f\u6570\uff0c\u4ee5\u5f97\u5230\u4e00\u4e2a5\u4f4d\u968f\u673a\u5c0f\u6570\u3002<\/p>\n<\/p>\n<p><p>\u56db\u3001\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u4ecb\u7ecd\uff0c\u53ef\u4ee5\u770b\u51fa\u5728Python\u4e2d\u751f\u62105\u4f4d\u968f\u673a\u5c0f\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u4f18\u7f3a\u70b9\u3002<strong>random\u6a21\u5757\u7b80\u5355\u6613\u7528\uff0c\u9002\u5408\u4e00\u822c\u573a\u666f\uff1bnumpy\u5e93\u529f\u80fd\u5f3a\u5927\uff0c\u9002\u5408\u79d1\u5b66\u8ba1\u7b97\u573a\u666f\uff1bdecimal\u6a21\u5757\u7cbe\u5ea6\u9ad8\uff0c\u9002\u5408\u9ad8\u7cbe\u5ea6\u8ba1\u7b97\u573a\u666f\u3002<\/strong>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u751f\u62105\u4f4d\u968f\u673a\u5c0f\u6570\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u90fd\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u751f\u62105\u4f4d\u968f\u673a\u5c0f\u6570\u7684\u76ee\u6807\u3002<\/p>\n<\/p>\n<p><p>\u4e94\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/p>\n<\/p>\n<p><p>\u751f\u6210\u968f\u673a\u5c0f\u6570\u5728\u8bb8\u591a\u5b9e\u9645\u5e94\u7528\u4e2d\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\u3002\u4f8b\u5982\uff0c\u5728\u91d1\u878d\u9886\u57df\u4e2d\uff0c\u9700\u8981\u751f\u6210\u968f\u673a\u7684\u5c0f\u6570\u7528\u4e8e\u6a21\u62df\u80a1\u7968\u4ef7\u683c\u53d8\u52a8\uff1b\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c\u9700\u8981\u751f\u6210\u968f\u673a\u7684\u5c0f\u6570\u7528\u4e8e\u6570\u636e\u91c7\u6837\uff1b\u5728\u8ba1\u7b97\u673a\u4eff\u771f\u4e2d\uff0c\u9700\u8981\u751f\u6210\u968f\u673a\u7684\u5c0f\u6570\u7528\u4e8e\u6a21\u62df\u7269\u7406\u73b0\u8c61\u7b49\u3002\u901a\u8fc7\u638c\u63e1\u751f\u6210\u968f\u673a\u5c0f\u6570\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\uff0c\u63d0\u5347\u7f16\u7a0b\u80fd\u529b\u3002<\/p>\n<\/p>\n<p><p>\u516d\u3001\u6269\u5c55\u5e94\u7528<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u751f\u62105\u4f4d\u968f\u673a\u5c0f\u6570\uff0c\u8fd8\u53ef\u4ee5\u6839\u636e\u9700\u6c42\u751f\u6210\u4efb\u610f\u4f4d\u6570\u7684\u968f\u673a\u5c0f\u6570\u3002\u53ea\u9700\u5c06\u4ee3\u7801\u4e2d\u768410\u76845\u6b21\u65b9\u66ff\u6362\u4e3a10\u7684n\u6b21\u65b9\uff0c\u5373\u53ef\u751f\u6210n\u4f4d\u968f\u673a\u5c0f\u6570\u3002\u4f8b\u5982\uff0c\u751f\u62106\u4f4d\u968f\u673a\u5c0f\u6570\u7684\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a0\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570<\/strong><\/h2>\n<p>random_float = random.random()<\/p>\n<h2><strong>\u5c06\u968f\u673a\u6d6e\u70b9\u6570\u4e58\u4ee5\u4e00\u4e2a\u5408\u9002\u7684\u500d\u6570\uff0c\u4fdd\u7559\u516d\u4f4d\u5c0f\u6570<\/strong><\/h2>\n<p>random_float_6_decimal = round(random_float * 10&lt;strong&gt;6) \/ 10&lt;\/strong&gt;6<\/p>\n<p>print(random_float_6_decimal)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u638c\u63e1\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u7075\u6d3b\u751f\u6210\u4efb\u610f\u4f4d\u6570\u7684\u968f\u673a\u5c0f\u6570\uff0c\u4ee5\u6ee1\u8db3\u4e0d\u540c\u573a\u666f\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u751f\u62105\u4f4d\u968f\u673a\u5c0f\u6570\u5728Python\u4e2d\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5b9e\u73b0\u3002<strong>\u901a\u8fc7\u4f7f\u7528random\u6a21\u5757\u3001numpy\u5e93\u548cdecimal\u6a21\u5757\uff0c\u53ef\u4ee5\u8f7b\u677e\u751f\u62105\u4f4d\u968f\u673a\u5c0f\u6570\u3002<\/strong>\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\uff0c\u5e76\u63d0\u5347\u7f16\u7a0b\u80fd\u529b\u3002\u751f\u6210\u968f\u673a\u5c0f\u6570\u5728\u8bb8\u591a\u5b9e\u9645\u5e94\u7528\u4e2d\u90fd\u6709\u5e7f\u6cdb\u7684\u5e94\u7528\uff0c\u638c\u63e1\u751f\u6210\u968f\u673a\u5c0f\u6570\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u89e3\u51b3\u5b9e\u9645\u95ee\u9898\uff0c\u63d0\u5347\u7f16\u7a0b\u80fd\u529b\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u5c0f\u6570\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>random.uniform(a, b)<\/code>\u51fd\u6570\u751f\u6210\u6307\u5b9a\u8303\u56f4[a, b]\u5185\u7684\u968f\u673a\u5c0f\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60a8\u5e0c\u671b\u751f\u62100\u52301\u4e4b\u95f4\u7684\u968f\u673a\u5c0f\u6570\uff0c\u53ef\u4ee5\u8fd9\u6837\u5199\uff1a<code>random.uniform(0, 1)<\/code>\u3002\u8fd9\u5c06\u8fd4\u56de\u4e00\u4e2a\u57280\u52301\u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<p><strong>\u751f\u6210\u7684\u968f\u673a\u5c0f\u6570\u53ef\u4ee5\u4fdd\u7559\u591a\u5c11\u4f4d\u5c0f\u6570\uff1f<\/strong><br \/>\u751f\u6210\u7684\u968f\u673a\u5c0f\u6570\u53ef\u4ee5\u901a\u8fc7\u4f7f\u7528<code>round()<\/code>\u51fd\u6570\u6765\u63a7\u5236\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u3002\u4f8b\u5982\uff0c\u5982\u679c\u60f3\u8981\u751f\u62105\u4f4d\u5c0f\u6570\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>round(random.uniform(0, 1), 5)<\/code>\u3002\u8fd9\u5c06\u786e\u4fdd\u60a8\u83b7\u5f97\u7684\u968f\u673a\u6570\u4fdd\u75595\u4f4d\u5c0f\u6570\u3002<\/p>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u751f\u6210\u591a\u4e2a\u968f\u673a\u5c0f\u6570\uff1f<\/strong><br \/>\u5f53\u7136\u53ef\u4ee5\uff01\u5982\u679c\u60a8\u5e0c\u671b\u751f\u6210\u591a\u4e2a\u968f\u673a\u5c0f\u6570\uff0c\u53ef\u4ee5\u4f7f\u7528\u5217\u8868\u63a8\u5bfc\u5f0f\u7ed3\u5408<code>random.uniform()<\/code>\u51fd\u6570\u3002\u4f8b\u5982\uff0c<code>random_numbers = [round(random.uniform(0, 1), 5) for _ in range(10)]<\/code>\u5c06\u751f\u6210\u4e00\u4e2a\u5305\u542b10\u4e2a\u968f\u673a\u5c0f\u6570\u7684\u5217\u8868\uff0c\u6bcf\u4e2a\u5c0f\u6570\u90fd\u4fdd\u75595\u4f4d\u5c0f\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u751f\u62105\u4f4d\u968f\u673a\u5c0f\u6570\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u5982\u4f7f\u7528random\u6a21\u5757\u3001numpy\u5e93\u3001\u4ee5\u53ca\u4f7f\u7528decimal\u6a21\u5757 [&hellip;]","protected":false},"author":3,"featured_media":1104498,"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\/1104469"}],"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=1104469"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104469\/revisions"}],"predecessor-version":[{"id":1104501,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1104469\/revisions\/1104501"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1104498"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1104469"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1104469"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1104469"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}