{"id":5452,"date":"2022-04-02T21:19:02","date_gmt":"2022-04-02T13:19:02","guid":{"rendered":"https:\/\/egonlin.com\/?p=5452"},"modified":"2022-04-02T21:19:32","modified_gmt":"2022-04-02T13:19:32","slug":"04-05-%e6%a8%a1%e5%9d%973-random%e5%ba%93%e7%9a%84%e4%bd%bf%e7%94%a8","status":"publish","type":"post","link":"https:\/\/egonlin.com\/?p=5452","title":{"rendered":"04-05 \u6a21\u57573-random\u5e93\u7684\u4f7f\u7528"},"content":{"rendered":"<h1>\u4e00\u3001random\u5e93\u57fa\u672c\u4ecb\u7ecd<\/h1>\n<p>random\u5e93\u662f\u4f7f\u7528\u968f\u673a\u6570\u7684Python\u6807\u51c6\u5e93<\/p>\n<ul>\n<li>\u4f2a\u968f\u673a\u6570\uff1a\u91c7\u7528\u6885\u68ee\u65cb\u8f6c\u7b97\u6cd5\u751f\u6210\u7684(\u4f2a)\u968f\u673a\u5e8f\u5217\u4e2d\u5143\u7d20<\/li>\n<li>random\u5e93\u4e3b\u8981\u7528\u4e8e\u751f\u6210\u968f\u673a\u6570<\/li>\n<li>\u4f7f\u7528random\u5e93\uff1a<code>import random<\/code><\/li>\n<\/ul>\n<h2>1.1 random\u5e93\u6982\u8ff0<\/h2>\n<p>random\u5e93\u5305\u62ec\u4e24\u7c7b\u51fd\u6570\uff0c\u5e38\u7528\u51718\u4e2a<\/p>\n<ul>\n<li>\u57fa\u672c\u968f\u673a\u6570\u51fd\u6570\uff1a <code>seed(), random()<\/code><\/li>\n<li>\u6269\u5c55\u968f\u673a\u6570\u51fd\u6570\uff1a<code>randint(), getrandbits(), uniform(),randrange(), choice(), shuffle()<\/code><\/li>\n<\/ul>\n<h1>\u4e8c\u3001\u57fa\u672c\u968f\u673a\u6570\u51fd\u6570<\/h1>\n<h2>2.1 \u968f\u673a\u6570\u79cd\u5b50<\/h2>\n<p><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/04\/random\u5e93\u7684\u4f7f\u75281.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/04\/random\u5e93\u7684\u4f7f\u75281.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\">\u51fd\u6570<\/th>\n<th style=\"text-align: center;\">\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">seed(a=None)<\/td>\n<td style=\"text-align: center;\">\u521d\u59cb\u5316\u7ed9\u5b9a\u7684\u968f\u673a\u6570\u79cd\u5b50\uff0c\u9ed8\u8ba4\u4e3a\u5f53\u524d\u7cfb\u7edf\u65f6\u95f4<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">random()<\/td>\n<td style=\"text-align: center;\">\u751f\u6210\u4e00\u4e2a[0.0, 1.0)\u4e4b\u95f4\u7684\u968f\u673a\u5c0f\u6570<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code class=\"language-python\">import random\n\nrandom.seed(10)  # \u4ea7\u751f\u79cd\u5b5010\u5bf9\u5e94\u7684\u5e8f\u5217\nrandom.random()\n0.5714025946899135\nrandom.random()\n0.4288890546751146\nrandom.seed(10)\nrandom.random()\n0.5714025946899135<\/code><\/pre>\n<h1>\u4e09\u3001\u6269\u5c55\u968f\u673a\u6570\u51fd\u6570<\/h1>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: center;\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/egonlin.com\/wp-content\/uploads\/2022\/04\/random\u5e93\u7684\u4f7f\u75282.png'><img class=\"lazyload lazyload-style-2\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  data-original=\"https:\/\/egonlin.com\/wp-content\/uploads\/2022\/04\/random\u5e93\u7684\u4f7f\u75282.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" \/><\/div><\/th>\n<th style=\"text-align: center;\">\u51fd\u6570<\/th>\n<th>\u63cf\u8ff0<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: center;\">randint(a,b)<\/td>\n<td style=\"text-align: center;\">\u751f\u6210\u4e00\u4e2a[a, b]\u4e4b\u95f4\u7684\u6574\u6570<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">randrange(m, n[, k])<\/td>\n<td style=\"text-align: center;\">\u751f\u6210\u4e00\u4e2a[m, n)\u4e4b\u95f4\u4ee5k\u4e3a\u6b65\u957f\u7684\u968f\u673a\u6574\u6570<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">getrandbits(k)<\/td>\n<td style=\"text-align: center;\">getrandbits(k)<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">uniform(a, b)<\/td>\n<td style=\"text-align: center;\">\u751f\u6210\u4e00\u4e2a[a, b]\u4e4b\u95f4\u7684\u968f\u673a\u5c0f\u6570<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">choice(seq)<\/td>\n<td style=\"text-align: center;\">\u4ece\u5e8f\u5217seq\u4e2d\u968f\u673a\u9009\u62e9\u4e00\u4e2a\u5143\u7d20<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: center;\">shuffle(seq)<\/td>\n<td style=\"text-align: center;\">\u5c06\u5e8f\u5217seq\u4e2d\u5143\u7d20\u968f\u673a\u6392\u5217\uff0c\u8fd4\u56de\u6253\u4e71\u540e\u7684\u5e8f\u5217<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<pre><code class=\"language-python\">import random\n\nrandom.randint(10, 100)\n64\nrandom.randrange(10, 100, 10)\n80\nrandom.getrandbits(16)\n37885\nrandom.uniform(10, 100)\n11.334920142209832\nrandom.choice([1, 2, 3, 4, 5, 6, 7])\n6\ns = [1, 2, 3, 4, 5, 6, 7, 8, 9]\nrandom.shuffle(s)\nprint(s)\n[3, 9, 7, 4, 1, 2, 6, 5, 8]<\/code><\/pre>\n<h2>3.1 \u968f\u673a\u6570\u51fd\u6570\u7684\u4f7f\u7528<\/h2>\n<p>\u9700\u8981\u638c\u63e1\u7684\u80fd\u529b<\/p>\n<ul>\n<li>\u80fd\u591f\u5229\u7528\u968f\u673a\u6570\u79cd\u5b50\u4ea7\u751f&quot;\u786e\u5b9a&quot;\u4f2a\u968f\u673a\u6570<\/li>\n<li>\u80fd\u591f\u4ea7\u751f\u968f\u673a\u6574\u6570<\/li>\n<li>\u80fd\u591f\u5bf9\u5e8f\u5217\u7c7b\u578b\u8fdb\u884c\u968f\u673a\u64cd\u4f5c<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001random\u5e93\u57fa\u672c\u4ecb\u7ecd random\u5e93\u662f\u4f7f\u7528\u968f\u673a\u6570\u7684Python\u6807\u51c6\u5e93 \u4f2a\u968f\u673a\u6570\uff1a\u91c7\u7528\u6885\u68ee\u65cb\u8f6c\u7b97\u6cd5\u751f\u6210\u7684( [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":5453,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[371,375],"tags":[],"_links":{"self":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/5452"}],"collection":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5452"}],"version-history":[{"count":0,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/posts\/5452\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=\/wp\/v2\/media\/5453"}],"wp:attachment":[{"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/egonlin.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}