{"id":4449,"date":"2020-03-29T13:50:45","date_gmt":"2020-03-29T13:50:45","guid":{"rendered":"https:\/\/www.askpython.com\/?p=4449"},"modified":"2022-08-06T13:28:30","modified_gmt":"2022-08-06T13:28:30","slug":"python-randint-method","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/python-randint-method","title":{"rendered":"Generate random integers using Python randint()"},"content":{"rendered":"\n<p>In this article, we&#8217;ll take a look at generating random integers using the Python <strong>randint()<\/strong> method.<\/p>\n\n\n\n<p>This method is in the random module in Python, which we will use to generate <strong>pseudo-random<\/strong> numbers, so we&#8217;ll need to import it to load this method. Let&#8217;s take a look at this method now!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Syntax of Python randint()<\/h2>\n\n\n\n<p>The Python randint() method returns a random integer between two limits <code>lower<\/code> and <code>upper<\/code>(inclusive of both limits). So this random number could also be one of the two limits.<\/p>\n\n\n\n<p>We can call this function as follows:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nrandom_integer = random.randint(lower, upper)\n<\/pre><\/div>\n\n\n<p>Here, <strong>lower<\/strong> is the lower limit of the random number, and <strong>upper<\/strong> is the upper limit of the random number.<\/p>\n\n\n\n<p>We must ensure that <code>lower<\/code> and <code>upper<\/code> are integers, and that <code>lower<\/code> &lt;= <code>upper<\/code>. Otherwise, a <code>ValueError<\/code> Exception will be raised.<\/p>\n\n\n\n<p>Let&#8217;s take a look at how we can use this function now.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Using the Python randint() method<\/h2>\n\n\n\n<p>We&#8217;ll need to import the <code>random<\/code> module. After that, we can call the function using the syntax.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport random\n\nbeg = 10\nend = 100\n\n# Generates a random integer between (10, 100)\nrandom_integer = random.randint(beg, end)\n\nprint(f&quot;A random integer between {beg} and {end} is: {random_integer}&quot;)\n<\/pre><\/div>\n\n\n<p><strong>Possible Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nA random integer between 10 and 100 is: 59\n<\/pre><\/div>\n\n\n<p>Indeed, we can see that this number does lie between the range (10, 100).<\/p>\n\n\n\n<p>If we want to repeat this pseudo-random generation, let&#8217;s use a <a href=\"https:\/\/www.askpython.com\/python\/python-loops-in-python\" class=\"rank-math-link\">loop<\/a> for that.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport random\n\nbeg = 0\nend = 100\n\nrand_list = &#x5B;random.randint(beg, end) for _ in range(10)]\n\nprint(rand_list) \n<\/pre><\/div>\n\n\n<p><strong>Possible Output<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&#x5B;61, 16, 39, 86, 25, 11, 16, 89, 99, 70]\n<\/pre><\/div>\n\n\n<p>We can see that these numbers are in the 0 to 100 range. And the pseudo-random conditions indicate that no two <strong>consecutive<\/strong> numbers repeat.<\/p>\n\n\n\n<p class=\"has-text-color has-background has-text-align-left has-very-light-gray-color has-vivid-cyan-blue-background-color\"><strong>NOTE<\/strong>: As I mentioned earlier, both <code>beg<\/code> and <code>end<\/code> must be integers, with <code>beg<\/code> &lt;= <code>end<\/code>. Otherwise, we will get a <code>ValueError<\/code> Exception.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this article, we learned how we could use the <code>randint()<\/code> method in Python, to generate random integers.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/docs.python.org\/3\/library\/random.html\" class=\"rank-math-link\" target=\"_blank\" rel=\"noopener\">Python Official Documentation<\/a> on the random module<\/li><li>JournalDev article on the randint() method<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator has-text-color has-background has-vivid-green-cyan-background-color has-vivid-green-cyan-color\"\/>\n","protected":false},"excerpt":{"rendered":"<p>In this article, we&#8217;ll take a look at generating random integers using the Python randint() method. This method is in the random module in Python, which we will use to generate pseudo-random numbers, so we&#8217;ll need to import it to load this method. Let&#8217;s take a look at this method now! Syntax of Python randint() [&hellip;]<\/p>\n","protected":false},"author":7,"featured_media":4468,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-4449","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4449","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/7"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=4449"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/4449\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/4468"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=4449"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=4449"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=4449"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}