{"id":965700,"date":"2024-12-27T04:40:31","date_gmt":"2024-12-26T20:40:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/965700.html"},"modified":"2024-12-27T04:40:33","modified_gmt":"2024-12-26T20:40:33","slug":"python%e5%a6%82%e4%bd%95%e5%af%bc%e5%85%a5random%e6%a8%a1%e5%9d%97","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/965700.html","title":{"rendered":"python\u5982\u4f55\u5bfc\u5165random\u6a21\u5757"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181857\/ccd2e06a-625d-411d-9b9b-d730a9cbf3fa.webp\" alt=\"python\u5982\u4f55\u5bfc\u5165random\u6a21\u5757\" \/><\/p>\n<p><p> Python\u4e2d\u7684<code>random<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\u548c\u6267\u884c\u968f\u673a\u9009\u62e9\u7b49\u64cd\u4f5c\u3002<strong>\u8981\u5728Python\u4e2d\u5bfc\u5165<code>random<\/code>\u6a21\u5757\uff0c\u53ea\u9700\u4f7f\u7528<code>import random<\/code>\u8bed\u53e5<\/strong>\u3002\u8fd9\u662fPython\u5185\u7f6e\u6a21\u5757\u7684\u4e00\u90e8\u5206\uff0c\u56e0\u6b64\u65e0\u9700\u989d\u5916\u5b89\u88c5\u3002<strong>\u5728\u5bfc\u5165\u540e\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528random\u6a21\u5757\u63d0\u4f9b\u7684\u5404\u79cd\u51fd\u6570\uff0c\u5982<code>random.randint()<\/code>\u3001<code>random.choice()<\/code>\u548c<code>random.shuffle()<\/code><\/strong>\u3002\u4e0b\u9762\uff0c\u6211\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\u548c\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001RANDOM\u6a21\u5757\u6982\u8ff0<\/p>\n<\/p>\n<p><p><code>random<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5404\u79cd\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\u7684\u51fd\u6570\uff0c\u8fd9\u4e9b\u51fd\u6570\u5e7f\u6cdb\u5e94\u7528\u4e8e\u6a21\u62df\u3001\u6e38\u620f\u3001\u6570\u636e\u5206\u6790\u7b49\u9886\u57df\u3002\u5b83\u53ef\u4ee5\u751f\u6210\u968f\u673a\u6574\u6570\u3001\u6d6e\u70b9\u6570\uff0c\u4ee5\u53ca\u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u5143\u7d20\u3002\u6b64\u5916\uff0c<code>random<\/code>\u6a21\u5757\u8fd8\u652f\u6301\u4f2a\u968f\u673a\u6570\u751f\u6210\u5668\u7684\u79cd\u5b50\u8bbe\u7f6e\uff0c\u4ee5\u786e\u4fdd\u5728\u76f8\u540c\u7684\u6761\u4ef6\u4e0b\u5f97\u5230\u76f8\u540c\u7684\u968f\u673a\u5e8f\u5217\u3002<\/p>\n<\/p>\n<p><p><strong>1. \u968f\u673a\u6574\u6570\u751f\u6210<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>random.randint(a, b)<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5728[a, b]\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002<code>a<\/code>\u548c<code>b<\/code>\u90fd\u662f\u5305\u542b\u5728\u5185\u7684\u8fb9\u754c\u3002\u4f8b\u5982\uff0c<code>random.randint(1, 10)<\/code>\u4f1a\u8fd4\u56de\u4e00\u4e2a1\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u5305\u62ec1\u548c10\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u968f\u673a\u6d6e\u70b9\u6570\u751f\u6210<\/strong><\/p>\n<\/p>\n<p><p><code>random<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86<code>random.random()<\/code>\u51fd\u6570\uff0c\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u5728[0.0, 1.0)\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u6b64\u5916\uff0c<code>random.uniform(a, b)<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5728[a, b]\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n<\/p>\n<p><p>\u4e8c\u3001RANDOM\u6a21\u5757\u7684\u5e38\u7528\u51fd\u6570<\/p>\n<\/p>\n<p><p><strong>1. random.randint()<\/strong><\/p>\n<\/p>\n<p><p><code>random.randint(a, b)<\/code>\u7528\u4e8e\u751f\u6210\u4e00\u4e2a\u5728[a, b]\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u975e\u5e38\u9002\u5408\u5728\u6e38\u620f\u4e2d\u968f\u673a\u751f\u6210\u89d2\u8272\u5c5e\u6027\u503c\u6216\u5728\u6a21\u62df\u4e2d\u521b\u5efa\u968f\u673a\u4e8b\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u751f\u6210\u4e00\u4e2a\u57281\u5230100\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570<\/strong><\/h2>\n<p>random_integer = random.randint(1, 100)<\/p>\n<p>print(f&quot;\u968f\u673a\u6574\u6570: {random_integer}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. random.choice()<\/strong><\/p>\n<\/p>\n<p><p><code>random.choice(seq)<\/code>\u4ece\u975e\u7a7a\u5e8f\u5217<code>seq<\/code>\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002\u5b83\u5e38\u7528\u4e8e\u4ece\u5217\u8868\u3001\u5143\u7ec4\u6216\u5b57\u7b26\u4e32\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u4ece\u5217\u8868\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20<\/strong><\/h2>\n<p>options = [&#39;\u77f3\u5934&#39;, &#39;\u526a\u5200&#39;, &#39;\u5e03&#39;]<\/p>\n<p>random_choice = random.choice(options)<\/p>\n<p>print(f&quot;\u968f\u673a\u9009\u62e9: {random_choice}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001RANDOM\u6a21\u5757\u7684\u9ad8\u7ea7\u7528\u6cd5<\/p>\n<\/p>\n<p><p><strong>1. \u968f\u673a\u6392\u5217\u5e8f\u5217<\/strong><\/p>\n<\/p>\n<p><p><code>random.shuffle(seq)<\/code>\u7528\u4e8e\u968f\u673a\u6392\u5217\u5e8f\u5217\u4e2d\u7684\u5143\u7d20\u3002\u8fd9\u4e2a\u51fd\u6570\u76f4\u63a5\u4fee\u6539\u539f\u5e8f\u5217\uff0c\u662f\u4e00\u79cd\u5b9e\u73b0\u6d17\u724c\u7b97\u6cd5\u7684\u7b80\u5355\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u5217\u8868<\/strong><\/h2>\n<p>numbers = [1, 2, 3, 4, 5]<\/p>\n<h2><strong>\u968f\u673a\u6253\u4e71\u5217\u8868<\/strong><\/h2>\n<p>random.shuffle(numbers)<\/p>\n<p>print(f&quot;\u6253\u4e71\u540e\u7684\u5217\u8868: {numbers}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p><strong>2. \u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50<\/strong><\/p>\n<\/p>\n<p><p>\u4f7f\u7528<code>random.seed(a=None)<\/code>\u53ef\u4ee5\u8bbe\u7f6e\u968f\u673a\u6570\u751f\u6210\u5668\u7684\u79cd\u5b50\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5728\u76f8\u540c\u7684\u79cd\u5b50\u4e0b\uff0c\u751f\u6210\u7684\u968f\u673a\u5e8f\u5217\u662f\u76f8\u540c\u7684\u3002\u79cd\u5b50\u7528\u4e8e\u4fdd\u8bc1\u7a0b\u5e8f\u7684\u53ef\u91cd\u590d\u6027\uff0c\u5c24\u5176\u5728\u8c03\u8bd5\u548c\u6d4b\u8bd5\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<h2><strong>\u8bbe\u7f6e\u968f\u673a\u6570\u79cd\u5b50<\/strong><\/h2>\n<p>random.seed(42)<\/p>\n<h2><strong>\u751f\u6210\u968f\u673a\u6570<\/strong><\/h2>\n<p>print(random.random())  # \u6bcf\u6b21\u8fd0\u884c\u7ed3\u679c\u76f8\u540c<\/p>\n<p>print(random.randint(1, 100))  # \u6bcf\u6b21\u8fd0\u884c\u7ed3\u679c\u76f8\u540c<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u5e94\u7528\u573a\u666f\u53ca\u6ce8\u610f\u4e8b\u9879<\/p>\n<\/p>\n<p><p><strong>1. \u6e38\u620f\u5f00\u53d1\u4e2d\u7684\u5e94\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\uff0c<code>random<\/code>\u6a21\u5757\u5e38\u7528\u4e8e\u968f\u673a\u751f\u6210\u602a\u7269\u5c5e\u6027\u3001\u6389\u843d\u7269\u54c1\u548c\u5730\u56fe\u751f\u6210\u7b49\u573a\u666f\u3002\u901a\u8fc7\u968f\u673a\u6570\uff0c\u6e38\u620f\u53ef\u4ee5\u53d8\u5f97\u66f4\u52a0\u4e30\u5bcc\u591a\u6837\u3002<\/p>\n<\/p>\n<p><p><strong>2. \u6570\u636e\u5206\u6790\u4e2d\u7684\u5e94\u7528<\/strong><\/p>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u4e2d\uff0c<code>random<\/code>\u6a21\u5757\u53ef\u4ee5\u7528\u4e8e\u62bd\u6837\u3001\u6570\u636e\u5206\u5272\u548c\u6a21\u62df\u5b9e\u9a8c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>random.sample()<\/code>\u53ef\u4ee5\u4ece\u4e00\u4e2a\u5e8f\u5217\u4e2d\u65e0\u653e\u56de\u5730\u968f\u673a\u62bd\u53d6\u6307\u5b9a\u6570\u91cf\u7684\u5143\u7d20\uff0c\u8fd9\u5bf9\u6570\u636e\u62bd\u6837\u975e\u5e38\u6709\u7528\u3002<\/p>\n<\/p>\n<p><p><strong>3. \u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<\/p>\n<p><p>\u867d\u7136<code>random<\/code>\u6a21\u5757\u751f\u6210\u7684\u662f\u4f2a\u968f\u673a\u6570\uff0c\u4f46\u5728\u5927\u591a\u6570\u5e94\u7528\u573a\u666f\u4e2d\u5df2\u7ecf\u8db3\u591f\u968f\u673a\u3002\u7136\u800c\uff0c\u5bf9\u4e8e\u9700\u8981\u9ad8\u5b89\u5168\u6027\u7684\u573a\u666f\uff0c\u5982\u5bc6\u7801\u751f\u6210\u6216\u52a0\u5bc6\u7b49\uff0c\u5efa\u8bae\u4f7f\u7528<code>secrets<\/code>\u6a21\u5757\uff0c\u8be5\u6a21\u5757\u63d0\u4f9b\u66f4\u9ad8\u5b89\u5168\u6027\u7684\u968f\u673a\u6570\u3002<\/p>\n<\/p>\n<p><p>\u603b\u7ed3\uff0cPython\u4e2d\u7684<code>random<\/code>\u6a21\u5757\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u80fd\u591f\u6ee1\u8db3\u5404\u79cd\u968f\u673a\u6570\u751f\u6210\u9700\u6c42\u3002\u901a\u8fc7\u638c\u63e1\u5176\u5e38\u7528\u51fd\u6570\u548c\u9ad8\u7ea7\u7528\u6cd5\uff0c\u5f00\u53d1\u8005\u53ef\u4ee5\u5728\u4e0d\u540c\u9886\u57df\u4e2d\u5b9e\u73b0\u4e30\u5bcc\u591a\u6837\u7684\u968f\u673a\u5316\u529f\u80fd\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5bfc\u5165random\u6a21\u5757\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u4f7f\u7528random\u6a21\u5757\uff0c\u60a8\u53ef\u4ee5\u5728\u4ee3\u7801\u7684\u5f00\u5934\u6dfb\u52a0\u4e00\u884c\u4ee3\u7801\uff1a<code>import random<\/code>\u3002\u8fd9\u884c\u4ee3\u7801\u4f1a\u5c06random\u6a21\u5757\u5f15\u5165\u5230\u60a8\u7684\u7a0b\u5e8f\u4e2d\uff0c\u4f7f\u60a8\u80fd\u591f\u8bbf\u95ee\u5176\u4e2d\u7684\u6240\u6709\u51fd\u6570\u548c\u529f\u80fd\u3002<\/p>\n<p><strong>random\u6a21\u5757\u63d0\u4f9b\u4e86\u54ea\u4e9b\u5e38\u7528\u529f\u80fd\uff1f<\/strong><br \/>random\u6a21\u5757\u5305\u542b\u591a\u79cd\u529f\u80fd\uff0c\u4f8b\u5982\u751f\u6210\u968f\u673a\u6570\u3001\u9009\u62e9\u968f\u673a\u5143\u7d20\u3001\u6253\u4e71\u5217\u8868\u7b49\u3002\u5e38\u7528\u7684\u51fd\u6570\u5305\u62ec<code>random.randint(a, b)<\/code>\u7528\u4e8e\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6574\u6570\uff0c<code>random.choice(seq)<\/code>\u4ece\u5e8f\u5217\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20\uff0c\u4ee5\u53ca<code>random.shuffle(x)<\/code>\u7528\u4e8e\u968f\u673a\u6253\u4e71\u5217\u8868\u7684\u987a\u5e8f\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528random\u6a21\u5757\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6d6e\u70b9\u6570\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>random.uniform(a, b)<\/code>\u51fd\u6570\u6765\u751f\u6210\u4e00\u4e2a\u5728\u7ed9\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u8fd9\u4e2a\u51fd\u6570\u4f1a\u8fd4\u56de\u4e00\u4e2a\u5927\u4e8e\u6216\u7b49\u4e8ea\u4e14\u5c0f\u4e8e\u6216\u7b49\u4e8eb\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u975e\u5e38\u9002\u5408\u9700\u8981\u6d6e\u70b9\u6570\u7684\u5e94\u7528\u573a\u666f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u7684random\u6a21\u5757\u662f\u4e00\u4e2a\u975e\u5e38\u6709\u7528\u7684\u5de5\u5177\uff0c\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\u548c\u6267\u884c\u968f\u673a\u9009\u62e9\u7b49\u64cd\u4f5c\u3002\u8981\u5728Python\u4e2d\u5bfc [&hellip;]","protected":false},"author":3,"featured_media":965711,"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\/965700"}],"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=965700"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/965700\/revisions"}],"predecessor-version":[{"id":965716,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/965700\/revisions\/965716"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/965711"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=965700"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=965700"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=965700"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}