{"id":303279,"date":"2024-05-20T18:56:14","date_gmt":"2024-05-20T10:56:14","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/303279.html"},"modified":"2024-05-20T18:56:41","modified_gmt":"2024-05-20T10:56:41","slug":"python-%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8-randrange-%e7%94%9f%e6%88%90%e9%9a%8f%e6%9c%ba%e6%95%b0","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/303279.html","title":{"rendered":"python \u5982\u4f55\u4f7f\u7528 randrange \u751f\u6210\u968f\u673a\u6570"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/26215459\/54152fd7-4975-4830-aa85-f45f3c411d91.webp\" alt=\"python \u5982\u4f55\u4f7f\u7528 randrange \u751f\u6210\u968f\u673a\u6570\" \/><\/p>\n<p><p>\u5728Python\u4e2d\uff0c\u4f7f\u7528<code>randrange<\/code>\u51fd\u6570\u751f\u6210\u968f\u673a\u6570\u65e2\u7b80\u4fbf\u53c8\u9ad8\u6548\u3002\u8be5\u65b9\u6cd5\u6765\u81ea\u4e8e<code>random<\/code>\u6a21\u5757\uff0c\u80fd\u591f\u8f7b\u677e\u5b9e\u73b0\u5728\u6307\u5b9a\u8303\u56f4\u5185\u751f\u6210\u4e00\u4e2a\u968f\u673a\u6574\u6570\u3002<strong>\u6838\u5fc3\u89c2\u70b9\u5305\u62ec\uff1a\u6307\u5b9a\u8d77\u59cb\u503c\u4e0e\u7ec8\u6b62\u503c\u3001\u53ef\u8bbe\u7f6e\u6b65\u957f\u3001\u6613\u4e8e\u5b9e\u73b0\u7279\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\u751f\u6210<\/strong>\u3002\u6b64\u5916\uff0c\u503c\u5f97\u4e00\u63d0\u7684\u662f\uff0c<code>randrange<\/code>\u7684\u7075\u6d3b\u6027\u5c24\u5176\u4f53\u73b0\u5728\u5176\u6b65\u957f\u8bbe\u7f6e\u4e0a\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u5728\u751f\u6210\u968f\u673a\u6570\u65f6\u6307\u5b9a\u4e00\u4e2a\u6b65\u957f\uff0c\u4ece\u800c\u5728\u4e00\u5b9a\u8303\u56f4\u5185\u751f\u6210\u7b26\u5408\u7279\u5b9a\u95f4\u9694\u7684\u968f\u673a\u6574\u6570\u3002\u4f8b\u5982\uff0c\u5728\u751f\u6210\u4e00\u4e2a0\u523010\u4e4b\u95f4\u7684\u5076\u6570\u65f6\uff0c\u53ef\u4ee5\u8bbe\u7f6e\u6b65\u957f\u4e3a2\u3002<\/p>\n<\/p>\n<p><h2>\u4e00\u3001\u968f\u673a\u6570\u751f\u6210\u7684\u57fa\u7840\u7406\u89e3<\/h2>\n<\/p>\n<p><p>\u5728\u6df1\u5165 randrange \u51fd\u6570\u4e4b\u524d\uff0c\u7406\u89e3Python\u968f\u673a\u6a21\u5757\u7684\u57fa\u672c\u6982\u5ff5\u662f\u5fc5\u8981\u7684\u3002<code>random<\/code>\u6a21\u5757\u5305\u542b\u4e86\u4e00\u7cfb\u5217\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570\u7684\u51fd\u6570\uff0c\u8fd9\u4e9b\u968f\u673a\u6570\u65e2\u53ef\u4ee5\u662f\u6574\u6570\u4e5f\u53ef\u4ee5\u662f\u6d6e\u70b9\u6570\u3002\u5176\u4e2d\uff0c<code>randrange<\/code>\u662f\u5b9e\u73b0\u6574\u6570\u968f\u673a\u751f\u6210\u7684\u6709\u529b\u5de5\u5177\u3002<\/p>\n<\/p>\n<p><p><code>random<\/code>\u6a21\u5757\u7684\u4f7f\u7528\u9996\u5148\u9700\u8981\u5bfc\u5165\u8be5\u6a21\u5757\uff0c\u4e4b\u540e\u5373\u53ef\u8c03\u7528\u5176\u5185\u7f6e\u7684\u5404\u79cd\u51fd\u6570\u6765\u5b8c\u6210\u4e0d\u540c\u7c7b\u578b\u7684\u968f\u673a\u6570\u751f\u6210\u3002\u5bfc\u5165\u6a21\u5757\u7684\u547d\u4ee4\u975e\u5e38\u7b80\u5355\uff0c\u53ea\u9700\u5728\u811a\u672c\u7684\u5f00\u5934\u4f7f\u7528<code>import random<\/code>\u5373\u53ef\u3002<\/p>\n<\/p>\n<p><h2>\u4e8c\u3001randrange \u51fd\u6570\u7684\u4f7f\u7528<\/h2>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>randrange<\/code>\u51fd\u6570\u751f\u6210\u968f\u673a\u6570\u3002<code>randrange<\/code>\u51fd\u6570\u7684\u57fa\u672c\u8bed\u6cd5\u4e3a\uff1a<code>random.randrange(start, stop[, step])<\/code>\uff0c\u5176\u4e2d<code>start<\/code>\u53c2\u6570\u8868\u793a\u8d77\u59cb\u503c\uff0c<code>stop<\/code>\u53c2\u6570\u8868\u793a\u7ec8\u6b62\u503c\u800c<code>step<\/code>\u53c2\u6570\u5219\u662f\u53ef\u9009\u7684\uff0c\u7528\u4e8e\u6307\u5b9a\u95f4\u9694\u3002<\/p>\n<\/p>\n<p><h3>1. \u57fa\u672c\u7528\u6cd5<\/h3>\n<\/p>\n<p><p>\u8981\u751f\u6210\u4e00\u4e2a\u4ece0\u523010\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u53ef\u4ee5\u7b80\u5355\u5730\u8c03\u7528<code>randrange<\/code>\u51fd\u6570\u800c\u4e0d\u9700\u8981\u989d\u5916\u6307\u5b9a\u6b65\u957f\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>number = random.randrange(0, 11)<\/p>\n<p>print(number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>2. \u4f7f\u7528\u6b65\u957f<\/h3>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u751f\u6210\u5177\u6709\u7279\u5b9a\u95f4\u9694\u7684\u968f\u673a\u6570\u65f6\uff0c\u6b65\u957f\u53c2\u6570\u663e\u5f97\u683c\u5916\u91cd\u8981\u3002\u901a\u8fc7\u8bbe\u7f6e\u6b65\u957f\uff0c\u53ef\u4ee5\u8f7b\u677e\u751f\u6210\u4f8b\u5982\u5076\u6570\u6216\u6ee1\u8db3\u5176\u4ed6\u7279\u5b9a\u6761\u4ef6\u7684\u968f\u673a\u6574\u6570\u3002\u4ee5\u4e0b\u793a\u4f8b\u6f14\u793a\u4e86\u5982\u4f55\u751f\u6210\u4e00\u4e2a0\u523010\u4e4b\u95f4\u7684\u968f\u673a\u5076\u6570\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import random<\/p>\n<p>even_number = random.randrange(0, 11, 2)<\/p>\n<p>print(even_number)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>\u4e09\u3001randrange \u4e0e\u5176\u4ed6\u968f\u673a\u6570\u751f\u6210\u51fd\u6570\u7684\u6bd4\u8f83<\/h2>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u5168\u9762\u5730\u7406\u89e3<code>randrange<\/code>\u51fd\u6570\u7684\u4f18\u52bf\u548c\u4f7f\u7528\u573a\u666f\uff0c\u4e0e<code>random<\/code>\u6a21\u5757\u4e2d\u7684\u5176\u4ed6\u968f\u673a\u6570\u751f\u6210\u65b9\u6cd5\u8fdb\u884c\u5bf9\u6bd4\u662f\u6709\u76ca\u7684\u3002\u4e3b\u8981\u53ef\u4ee5\u6bd4\u8f83<code>randint<\/code>\u548c<code>randrange<\/code>\u3001<code>uniform<\/code>\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><h3>1. randrange \u4e0e randint<\/h3>\n<\/p>\n<p><p>\u867d\u7136<code>randint<\/code>\u548c<code>randrange<\/code>\u90fd\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6574\u6570\uff0c\u4f46<code>randrange<\/code>\u63d0\u4f9b\u4e86\u6b65\u957f\u53c2\u6570\uff0c\u589e\u52a0\u4e86\u751f\u6210\u968f\u673a\u6570\u7684\u7075\u6d3b\u6027\u3002<code>randint<\/code>\u53ea\u80fd\u751f\u6210\u4e24\u4e2a\u53c2\u6570\u4e4b\u95f4\u7684\u968f\u673a\u6574\u6570\uff0c\u800c\u65e0\u6cd5\u8bbe\u5b9a\u6b65\u957f\u3002<\/p>\n<\/p>\n<p><h3>2. randrange \u4e0e uniform<\/h3>\n<\/p>\n<p><p><code>uniform<\/code>\u51fd\u6570\u7528\u4e8e\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002\u5982\u679c\u9700\u8981\u751f\u6210\u7684\u662f\u6d6e\u70b9\u6570\u800c\u4e0d\u662f\u6574\u6570\uff0c\u5219\u5e94\u9009\u62e9<code>uniform<\/code>\u800c\u4e0d\u662f<code>randrange<\/code>\u3002<\/p>\n<\/p>\n<p><h2>\u56db\u3001\u5e94\u7528\u573a\u666f\u4e0e\u5b9e\u9645\u6848\u4f8b<\/h2>\n<\/p>\n<p><p><code>randrange<\/code>\u51fd\u6570\u80fd\u591f\u5e94\u7528\u4e8e\u5404\u79cd\u9700\u8981\u968f\u673a\u6570\u751f\u6210\u7684\u573a\u666f\u4e2d\uff0c\u7279\u522b\u662f\u5728\u9700\u8981\u63a7\u5236\u6570\u5b57\u95f4\u9694\u65f6\u3002\u4e0b\u9762\u901a\u8fc7\u5b9e\u9645\u7684\u6848\u4f8b\u6765\u8fdb\u4e00\u6b65\u5c55\u793a<code>randrange<\/code>\u7684\u5b9e\u7528\u6027\u3002<\/p>\n<\/p>\n<p><h3>1. \u6e38\u620f\u5f00\u53d1<\/h3>\n<\/p>\n<p><p>\u5728\u6e38\u620f\u5f00\u53d1\u4e2d\uff0c\u968f\u673a\u6570\u7684\u751f\u6210\u53ef\u4ee5\u7528\u4e8e\u786e\u5b9a\u89d2\u8272\u7684\u521d\u59cb\u4f4d\u7f6e\u3001\u968f\u673a\u4e8b\u4ef6\u7684\u89e6\u53d1\u7b49\u3002\u4f7f\u7528<code>randrange<\/code>\u53ef\u4ee5\u65b9\u4fbf\u5730\u751f\u6210\u6ee1\u8db3\u7279\u5b9a\u6761\u4ef6\u7684\u968f\u673a\u6570\uff0c\u5982\u968f\u673a\u7684\u6b65\u957f\u589e\u52a0\u4e86\u6570\u503c\u751f\u6210\u7684\u591a\u6837\u6027\u548c\u6e38\u620f\u7684\u53ef\u73a9\u6027\u3002<\/p>\n<\/p>\n<p><h3>2. \u6570\u636e\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5206\u6790\u7684\u6a21\u62df\u5b9e\u9a8c\u4e2d\uff0c\u751f\u6210\u4e00\u7cfb\u5217\u6ee1\u8db3\u7279\u5b9a\u95f4\u9694\u7684\u968f\u673a\u6570\u53ef\u4ee5\u6a21\u62df\u4e0d\u540c\u7684\u5b9e\u9a8c\u6761\u4ef6\u3002<code>randrange<\/code>\u7684\u6b65\u957f\u53c2\u6570\u5728\u8fd9\u91cc\u53ef\u4ee5\u7528\u6765\u63a7\u5236\u6570\u636e\u7684\u7c92\u5ea6\uff0c\u8fdb\u800c\u5f71\u54cd\u5b9e\u9a8c\u7684\u8bbe\u8ba1\u548c\u7ed3\u679c\u3002<\/p>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0c<code>randrange<\/code>\u4f5c\u4e3aPython\u4e2d<code>random<\/code>\u6a21\u5757\u7684\u4e00\u90e8\u5206\uff0c\u4ee5\u5176\u7075\u6d3b\u6027\u548c\u65b9\u4fbf\u6027\uff0c\u5728\u4f17\u591a\u9700\u8981\u968f\u673a\u6574\u6570\u751f\u6210\u7684\u573a\u666f\u4e2d\u5360\u636e\u4e86\u91cd\u8981\u7684\u4f4d\u7f6e\u3002\u901a\u8fc7\u6b63\u786e\u7684\u4f7f\u7528<code>randrange<\/code>\uff0c\u53ef\u4ee5\u5728\u4fdd\u8bc1\u968f\u673a\u6027\u7684\u540c\u65f6\uff0c\u589e\u52a0\u751f\u6210\u6570\u503c\u7684\u63a7\u5236\u6027\u548c\u9002\u5e94\u6027\uff0c\u662fPython\u7f16\u7a0b\u4e2d\u4e0d\u53ef\u6216\u7f3a\u7684\u5de5\u5177\u4e4b\u4e00\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u5982\u4f55\u5728 Python \u4e2d\u4f7f\u7528 randrange \u51fd\u6570\u751f\u6210\u6307\u5b9a\u8303\u56f4\u7684\u968f\u673a\u6570\uff1f<\/strong><\/p>\n<p>randrange \u51fd\u6570\u662f Python \u4e2d\u5e38\u7528\u7684\u751f\u6210\u968f\u673a\u6570\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002\u5b83\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u6574\u6570\u968f\u673a\u6570\u3002\u4f7f\u7528\u65b9\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">from random import randrange\n\n# \u751f\u6210\u8303\u56f4\u5728 1 \u5230 10 \u7684\u968f\u673a\u6574\u6570\nrandom_number = randrange(1, 11)\nprint(random_number)\n<\/code><\/pre>\n<p>\u4f7f\u7528 <code>randrange(start, stop)<\/code>\uff0c\u5176\u4e2d <code>start<\/code> \u8868\u793a\u968f\u673a\u6570\u7684\u4e0b\u754c\uff0c<code>stop<\/code> \u8868\u793a\u968f\u673a\u6570\u7684\u4e0a\u754c\uff08\u4e0d\u5305\u62ec\uff09\u3002\u4e0a\u8ff0\u4ee3\u7801\u4f1a\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u5728 1 \u5230 10 \u4e4b\u95f4\uff08\u5305\u62ec 1\u3001\u4e0d\u5305\u62ec 10\uff09\u7684\u968f\u673a\u6574\u6570\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002<\/p>\n<p><strong>2. \u5982\u4f55\u751f\u6210\u6307\u5b9a\u6b65\u957f\u7684\u968f\u673a\u6570\u5e8f\u5217\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u7684\u968f\u673a\u6570\uff0crandrange \u51fd\u6570\u8fd8\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u6b65\u957f\u7684\u968f\u673a\u6570\u5e8f\u5217\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">from random import randrange\n\n# \u751f\u6210\u8303\u56f4\u5728 1 \u5230 10\uff0c\u6b65\u957f\u4e3a 2 \u7684\u968f\u673a\u6574\u6570\u5e8f\u5217\nrandom_numbers = [randrange(1, 11, 2) for _ in range(5)]\nprint(random_numbers)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4f7f\u7528\u4e86\u5217\u8868\u63a8\u5bfc\u5f0f\u751f\u6210\u4e86\u4e00\u4e2a\u5305\u542b 5 \u4e2a\u8303\u56f4\u5728 1 \u5230 10 \u4e4b\u95f4\u3001\u6b65\u957f\u4e3a 2 \u7684\u968f\u673a\u6574\u6570\u5e8f\u5217\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002<\/p>\n<p><strong>3. \u5982\u4f55\u4f7f\u7528 randrange \u751f\u6210\u968f\u673a\u7684\u6d6e\u70b9\u6570\uff1f<\/strong><\/p>\n<p>\u9664\u4e86\u53ef\u4ee5\u751f\u6210\u968f\u673a\u7684\u6574\u6570\uff0crandrange \u51fd\u6570\u8fd8\u53ef\u4ee5\u751f\u6210\u968f\u673a\u7684\u6d6e\u70b9\u6570\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">from random import uniform\n\n# \u751f\u6210\u8303\u56f4\u5728 0.0 \u5230 1.0 \u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\nrandom_float = uniform(0.0, 1.0)\nprint(random_float)\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4f7f\u7528\u4e86 uniform \u51fd\u6570\u6765\u751f\u6210\u4e00\u4e2a\u8303\u56f4\u5728 0.0 \u5230 1.0 \u4e4b\u95f4\u7684\u968f\u673a\u6d6e\u70b9\u6570\uff0c\u5e76\u5c06\u5176\u6253\u5370\u51fa\u6765\u3002uniform \u51fd\u6570\u7684\u7528\u6cd5\u7c7b\u4f3c\u4e8e randrange\uff0c\u4f46\u662f\u5b83\u53ef\u4ee5\u751f\u6210\u6307\u5b9a\u8303\u56f4\u5185\u7684\u968f\u673a\u6d6e\u70b9\u6570\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4f7f\u7528randrange\u51fd\u6570\u751f\u6210\u968f\u673a\u6570\u65e2\u7b80\u4fbf\u53c8\u9ad8\u6548\u3002\u8be5\u65b9\u6cd5\u6765\u81ea\u4e8erandom\u6a21\u5757\uff0c\u80fd\u591f\u8f7b\u677e\u5b9e\u73b0 [&hellip;]","protected":false},"author":3,"featured_media":303316,"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\/303279"}],"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=303279"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/303279\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/303316"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=303279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=303279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=303279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}