{"id":1493,"date":"2018-10-29T06:18:30","date_gmt":"2018-10-29T05:18:30","guid":{"rendered":"https:\/\/pythonprogramming.altervista.org\/?p=1493"},"modified":"2018-10-29T06:18:30","modified_gmt":"2018-10-29T05:18:30","slug":"create-random-numbers","status":"publish","type":"post","link":"https:\/\/pythonprogramming.altervista.org\/create-random-numbers\/","title":{"rendered":"Create random numbers"},"content":{"rendered":"<h1>How can I generate integer random numbers?<\/h1>\n<p>I can create random numbers with the random module in Python.<\/p>\n<p>First import the module like this<\/p>\n<pre class=\"lang:default decode:true \"># generate random numbers\r\n# module to import\r\nimport random<\/pre>\n<p>Alternatively, you can <strong>import<\/strong> like this, so that you don&#8217;t have to use the name of the module before the name of the functions:<\/p>\n<pre class=\"lang:default decode:true\">from random import randint<\/pre>\n<p>Now, we can use different <strong>functions<\/strong> from this module to generate random numbers. Let&#8217;s see the random functions for <strong>integers<\/strong>.<\/p>\n<h2>Functions for integers<a class=\"headerlink\" title=\"Permalink to this headline\" href=\"https:\/\/docs.python.org\/3\/library\/random.html#functions-for-integers\">\u00b6<\/a><\/h2>\n<dl class=\"function\">\n<dt id=\"random.randrange\"><code class=\"descclassname\">random.<\/code><code class=\"descname\">randrange<\/code><span class=\"sig-paren\">(<\/span><em>stop<\/em><span class=\"sig-paren\">)<\/span><\/dt>\n<dt>This function will generate a random integer number from 0 to stop (any number)<\/dt>\n<dt>\n<pre class=\"lang:default decode:true\">from random import randrange\r\n\r\n&gt;&gt;&gt; randrange(100)\r\n82\r\n&gt;&gt;&gt;<\/pre>\n<\/dt>\n<dt><code class=\"descclassname\">random.<\/code><code class=\"descname\">randrange<\/code><span class=\"sig-paren\">(<\/span><em>start<\/em>,\u00a0<em>stop<\/em><span class=\"optional\">[<\/span>,\u00a0<em>step<\/em><span class=\"optional\">]<\/span><span class=\"sig-paren\">)<\/span><\/dt>\n<dt>With start, stop and step (optional arguments), you can say wich can be the littles number and the interval between the number of choice in the interval. For example randrange(10, 100, 20) will return a number among 10 30 50 70 90, starting from 10 and with an interval of 20 each.<\/dt>\n<dt>\n<pre class=\"lang:default decode:true \">&gt;&gt;&gt; print([randrange(10,100,20) for x in range(3)])\r\n[90, 70, 10]<\/pre>\n<p>The example above shows a for loop where the randrange function is used 3 times and, as you can see in the result, we have 3 numbers all with a &#8216;distance&#8217; of 20 (the step chosen as third optional argument)<span style=\"color: #333333;\">.<\/span><\/dt>\n<\/dl>\n<dl class=\"function\">\n<dt id=\"random.randint\"><code class=\"descclassname\">random.<\/code><code class=\"descname\">randint<\/code><span class=\"sig-paren\">(<\/span><em>a<\/em>,\u00a0<em>b<\/em><span class=\"sig-paren\">)<\/span><\/dt>\n<dt>This is like randrange, but hasn&#8217;t the step argument and must have always the start and stop parameter.<\/dt>\n<dt>\n<pre class=\"lang:default decode:true \">from random import randint\r\n\r\n&gt;&gt;&gt; for n in range(10):\r\n...  randint(1,10)\r\n...\r\n4\r\n6\r\n9\r\n9\r\n8\r\n3\r\n9\r\n4\r\n10\r\n6\r\n&gt;&gt;&gt;<\/pre>\n<p>&nbsp;<\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"Random numbers, how to generate them, for integers\n<a class=\"moretag\" href=\"https:\/\/pythonprogramming.altervista.org\/create-random-numbers\/\"> [...]<\/a>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","footnotes":""},"categories":[1],"tags":[],"class_list":["post-1493","post","type-post","status-publish","format-standard","hentry","category-examples"],"avopt_banners_inside_post":true,"avopt_banners_on_page":true,"av_copy_from":"","av_sharing_message":"","av_sharing_allowed":true,"av_sharing_on":{"fb":[],"tw":[]},"av_allow_affiliate_banner":false,"av_allow_affiliate_multi_banner":false,"av_show_affiliation_buy_button":false,"av_post_rating":true,"av_have_post_rating_value":false,"av_is_artificial_intelligence_content":false,"_links":{"self":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1493","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/comments?post=1493"}],"version-history":[{"count":2,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1493\/revisions"}],"predecessor-version":[{"id":1495,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/posts\/1493\/revisions\/1495"}],"wp:attachment":[{"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/media?parent=1493"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/categories?post=1493"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pythonprogramming.altervista.org\/wp-json\/wp\/v2\/tags?post=1493"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}