{"id":79369,"date":"2020-07-04T09:00:11","date_gmt":"2020-07-04T03:30:11","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=79369"},"modified":"2020-07-04T09:00:11","modified_gmt":"2020-07-04T03:30:11","slug":"python-random-number-generator","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/","title":{"rendered":"Python Random Number Generator &#8211; Python Random Module"},"content":{"rendered":"<p>Ever wondered how you get a random dice number every time you play online Ludo. Or how shuffling your song playlist works?<\/p>\n<p>Turns out, your gaming or music app generates random numbers underneath the hood. And based on this random number, it decides which dice number to display or which song to play.<\/p>\n<p>But doesn\u2019t \u201crandom\u201d shuffling your playlist seems far from random? This is because these random numbers are not truly random, rather they are pseudo-random numbers. A<strong> pseudo-random number<\/strong> generator (PRNG) is an <strong>algorithm<\/strong> that generates seemingly random numbers. Let&#8217;s learn about Python Random Number Generator.<\/p>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/07\/generating-random-numbers-in-python.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-79370\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/07\/generating-random-numbers-in-python.jpg\" alt=\"python random number generator\" width=\"802\" height=\"420\" \/><\/a><\/p>\n<h2>Python Random Module<\/h2>\n<p>Python generates these pseudo-random numbers using the random module. But if you head to the Python docs for the documentation of this module, you\u2019ll see a warning there &#8211;<\/p>\n<p><strong>\u201cThe pseudo-random generators of this module should not be used for security purposes.\u201d<\/strong><\/p>\n<p>Apparently, the pseudo-random numbers that the random module generates are not cryptographically secure. Python uses Mersenne Twister algorithm for generating random numbers. But this algorithm is completely deterministic, making it an unsuitable choice for cryptographic purposes.<\/p>\n<p>Now let\u2019s move on to various built-in functions, under the random module. These functions are capable of generating pseudo-random numbers under different scenarios.<\/p>\n<h2>Built-in Functions for Python Random Number Generator<\/h2>\n<p><a href=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/07\/Built-in-functions-for-generating-random-numbers-in-python-Copy.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-79371\" src=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/sites\/2\/2020\/07\/Built-in-functions-for-generating-random-numbers-in-python-Copy.jpg\" alt=\"Built-in functions for generating random numbers in python\" width=\"644\" height=\"476\" \/><\/a><\/p>\n<p>The list below contains a brief description of the functions we\u2019re going to cover for random number generation.<\/p>\n<table>\n<tbody>\n<tr>\n<td><b>Function<\/b><\/td>\n<td><b>What it does<\/b><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">randint(x, y)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Generates a random integer from <\/span><span style=\"font-weight: 400\">x <\/span><span style=\"font-weight: 400\">to <\/span><span style=\"font-weight: 400\">y<\/span><span style=\"font-weight: 400\">, including the <\/span><span style=\"font-weight: 400\">x and y.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">randrange(start, stop, step)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Generates a random integer in the range(<\/span><span style=\"font-weight: 400\">start, stop, step<\/span><span style=\"font-weight: 400\">)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">random()<\/span><\/td>\n<td><span style=\"font-weight: 400\">Generates a random floating-point number in the interval [0,1)<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">uniform(x, y)<\/span><\/td>\n<td><span style=\"font-weight: 400\">It generates a floating-point value between <\/span><span style=\"font-weight: 400\">x<\/span><span style=\"font-weight: 400\"> and <\/span><span style=\"font-weight: 400\">y<\/span><span style=\"font-weight: 400\">.\u00a0<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">sample(population, k)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Selects <\/span><span style=\"font-weight: 400\">k <\/span><span style=\"font-weight: 400\">unique random elements from a <\/span><span style=\"font-weight: 400\">population <\/span><span style=\"font-weight: 400\">sequence or set.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">choice(seq)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Chooses a random element from a non-empty sequence <\/span><span style=\"font-weight: 400\">seq<\/span><span style=\"font-weight: 400\">.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">shuffle(x)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Shuffles list <\/span><span style=\"font-weight: 400\">x <\/span><span style=\"font-weight: 400\">in place.<\/span><\/td>\n<\/tr>\n<tr>\n<td><span style=\"font-weight: 400\">seed(x)<\/span><\/td>\n<td><span style=\"font-weight: 400\">Generates the same sequence of random numbers every time you call<\/span><span style=\"font-weight: 400\"> seed(x).<\/span><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Let\u2019s now dive deeper into each of these functions.<\/p>\n<h3>1. Python randint()<\/h3>\n<p>This function generates an integer between the specified limits. It takes two arguments x and y and produces integer i such that x &lt;= i &lt;= y.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import random\n&gt;&gt;&gt; random.randint(3, 6)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">4<br \/>\n&gt;&gt;&gt;<\/div>\n<h3>2. Python randrange()<\/h3>\n<p>This function takes 2 arguments start and stop along with 1 optional argument step. And returns a random integer in the range(start, stop, step). The default value of step is 1.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import random\n&gt;&gt;&gt; random.randrange(1, 10, 2)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">3<br \/>\n&gt;&gt;&gt;<\/div>\n<p>In this example, the output will be a random integer from [1, 3, 5, 7, 9] as the start and stop values are 1 and 10 respectively and the step value is 2.<\/p>\n<h3>3. Python random()<\/h3>\n<p>This function returns a random floating-point number between 0 and 1 (excluding 0 and 1).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import random\n&gt;&gt;&gt; random.random()\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">0.5313843048739985<br \/>\n&gt;&gt;&gt;<\/div>\n<h3>4. Python uniform()<\/h3>\n<p>This function lets you generate a floating-point number within a specific limit. It takes two arguments to start and stop and then returns a float between the start and stop (including the limits).<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import random\n&gt;&gt;&gt; random.uniform(6, 9)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">6.338126781525701<br \/>\n&gt;&gt;&gt;<\/div>\n<h3>5. Python choice()<\/h3>\n<p>If you want to choose a random element from a specific sequence, you can use this function. It takes one argument &#8211; the sequence. And it returns a random element from the sequence.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import random\n&gt;&gt;&gt; seq = (12, 33, 67, 55, 78, 90, 34, 67, 88)\n&gt;&gt;&gt; random.choice(seq)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">67<br \/>\n&gt;&gt;&gt;<\/div>\n<h3>6. Python sample()<\/h3>\n<p>If you want more than one random element from a sequence, you can use sample(). It takes two arguments population and k, where population is a sequence and k is an integer. Then it returns a list of k random elements from the sequence population.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import random\n&gt;&gt;&gt; seq = (12, 33, 67, 55, 78, 90, 34, 67, 88)\n&gt;&gt;&gt; random.sample(seq, 5)\n\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[33, 90, 78, 88, 12]<br \/>\n&gt;&gt;&gt;<\/div>\n<h3>7. Python shuffle()<\/h3>\n<p>This function takes one argument &#8211; a list. It then shuffles the elements of the list in place and returns None.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; import random\n&gt;&gt;&gt; l = [10, 20, 30, 40, 50]\n&gt;&gt;&gt; random.shuffle(l)\n&gt;&gt;&gt; print(l)\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">[20, 40, 30, 50, 10]<\/div>\n<h3>8. Python\u00a0 seed()<\/h3>\n<p>You can use this function when you need to generate the same sequence of random numbers multiple times. It takes one argument- the seed value. This value initializes a pseudo-random number generator. Now, whenever you call the seed() function with this seed value, it will produce the exact same sequence of random numbers.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">import random\n# seed value = 3\nrandom.seed(3)\nfor i in range(3):\n    print(random.random(), end = ' ')\n\nprint('\\n')\n\n# seed value = 8\nrandom.seed(8)\nfor i in range(3):\n    print(random.random(), end = ' ')\n\nprint('\\n')\n\n# seed value again = 3\nrandom.seed(3)\nfor i in range(3):\n    print(random.random(), end = ' ')\n\nprint('\\n')\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<div class=\"code-output\">\n<p>0.23796462709189137 0.5442292252959519 0.369955166548079250.2267058593810488 0.9622950358343828 0.12633089865085956<\/p>\n<p>0.23796462709189137 0.5442292252959519 0.36995516654807925<\/p>\n<p>&gt;&gt;&gt;<\/p>\n<\/div>\n<p>Notice that for seed = 3, the same sequence gets produced every time.<\/p>\n<h2>Summary<\/h2>\n<p>In this article, we looked at some majorly used functions which are capable of generating pseudo-random numbers in Python. We saw the functions we can use for generating integers. We also saw how to generate random floating-point values. Lastly, we saw how we can use built-in functions to select random elements out of a sequence. You can build various small projects using random numbers. You can build games, you can use random numbers while working with statistics in Data Science and much more. Use your creativity and play with numbers!<\/p>\n<p><strong>Hope you enjoyed the Python Random Number Generator tutorial!!<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Ever wondered how you get a random dice number every time you play online Ludo. Or how shuffling your song playlist works? Turns out, your gaming or music app generates random numbers underneath the&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":79370,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1053],"tags":[3016,3017,3018],"class_list":["post-79369","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-python-random","tag-python-random-number","tag-python-random-number-generator"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Python Random Number Generator - Python Random Module - TechVidvan<\/title>\n<meta name=\"description\" content=\"Learn about Python Random Number Generator &amp; random module - Functions for generating random number in python - randint(),randrange(), random(), uniform(),choice(),sample(),shuffle(),seed(),\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Random Number Generator - Python Random Module - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Learn about Python Random Number Generator &amp; random module - Functions for generating random number in python - randint(),randrange(), random(), uniform(),choice(),sample(),shuffle(),seed(),\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-07-04T03:30:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/generating-random-numbers-in-python.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Random Number Generator - Python Random Module - TechVidvan","description":"Learn about Python Random Number Generator & random module - Functions for generating random number in python - randint(),randrange(), random(), uniform(),choice(),sample(),shuffle(),seed(),","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/","og_locale":"en_US","og_type":"article","og_title":"Python Random Number Generator - Python Random Module - TechVidvan","og_description":"Learn about Python Random Number Generator & random module - Functions for generating random number in python - randint(),randrange(), random(), uniform(),choice(),sample(),shuffle(),seed(),","og_url":"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-07-04T03:30:11+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/generating-random-numbers-in-python.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Python Random Number Generator &#8211; Python Random Module","datePublished":"2020-07-04T03:30:11+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/"},"wordCount":785,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/generating-random-numbers-in-python.jpg","keywords":["python random","python random number","Python Random Number Generator"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/","url":"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/","name":"Python Random Number Generator - Python Random Module - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/generating-random-numbers-in-python.jpg","datePublished":"2020-07-04T03:30:11+00:00","description":"Learn about Python Random Number Generator & random module - Functions for generating random number in python - randint(),randrange(), random(), uniform(),choice(),sample(),shuffle(),seed(),","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/generating-random-numbers-in-python.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/07\/generating-random-numbers-in-python.jpg","width":802,"height":420,"caption":"python random number generator"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/python-random-number-generator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Python Random Number Generator &#8211; Python Random Module"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79369","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=79369"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79369\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/79370"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=79369"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=79369"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=79369"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}