{"id":2623,"date":"2022-02-04T11:32:08","date_gmt":"2022-02-04T06:02:08","guid":{"rendered":"https:\/\/cbsepython.in\/?p=2623"},"modified":"2026-01-05T13:51:59","modified_gmt":"2026-01-05T08:21:59","slug":"random-module-in-python-class-11-12-notes","status":"publish","type":"post","link":"https:\/\/cbsepython.in\/random-module-in-python-class-11-12-notes\/","title":{"rendered":"Random Module in Python Class 11-12 Notes"},"content":{"rendered":"<h1><span style=\"color: #000000;\">Random Module in Python Class 11-12 Notes<\/span><\/h1>\n<p>The <strong data-start=\"137\" data-end=\"164\">Random module in Python<\/strong> is an important topic in the <strong data-start=\"194\" data-end=\"236\">Class 11 and Class 12 Computer Science<\/strong> syllabus and is widely used in practical programming. It provides built-in functions to generate <strong data-start=\"334\" data-end=\"390\">random numbers, random choices, and random sequences<\/strong>, which are useful in games, simulations, and data-related programs. Understanding the Random module is essential for <strong data-start=\"508\" data-end=\"564\">board exams, practical files, and competitive coding<\/strong>, as per <strong data-start=\"573\" data-end=\"602\">CBSE and NCERT guidelines<\/strong>.<\/p>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Random numbers are widely used in programs which involves games or simulations. Some uses of random numbers are as follows:<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666 To shuffle a deck of playing cards randomly<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666 To play a game of chances where the computer needs to throw some dice, pick a number randomly or flip a coin.<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666 Generation of captcha<\/span><\/p>\n<p><span style=\"color: #000000;\">\u2666 Random selection of winner in lucky draw contest.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">How to use random module?<\/span><\/h3>\n<p><span style=\"color: #000000;\">To use random module, firstly you should import random at the beginning of your code.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">import random<\/span><\/h3>\n<p><span style=\"color: #000000;\">There are many functions such as random(), randrange(), randint(), shuffle(), uniform(), and choice() in random module. Here we learn the working of\u00a0 these functions.<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">1. random( )<\/span><\/h3>\n<p><span style=\"color: #000000;\">This function generates random numbers between 0 and 1. It means the result will be between 0 or less than 1 (including 0, but excluding 1). It will never give 1 as output means output may be 0, 0.123,0.9999 etc. It will generate different number on each execution. This function can be used to generate pseudorandom <em><strong>floating point<\/strong><\/em> values.<\/span><\/p>\n<p><span style=\"color: #000000;\">Example:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import random\r\nrandom_number=random.random()\r\nprint(random_number)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><strong><span style=\"color: #000000;\">Output:<\/span><\/strong><\/p>\n<p><span style=\"color: #000000;\">First Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">0.319979864671<\/span><\/pre>\n<p><span style=\"color: #000000;\">Second Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">0.582918157917<\/span><\/pre>\n<p><span style=\"color: #000000;\">Third Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">0.55455335024<\/span><\/pre>\n<p><span style=\"color: #000000;\">Fourth Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">0.547415686776<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">2. randrange()<\/span><\/h3>\n<p><span style=\"color: #000000;\">This function generate random integer numbers between ranges (lower and upper argument).<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><strong><span style=\"color: #000000;\">Ex 1: random_number=random.randrange(10)<\/span><\/strong><\/p>\n<p><span style=\"color: #000000;\">The above code will generate random number between 0 to 9. (as we know indexing)<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import random\r\nrandom_number=random.randrange(10)\r\nprint(random_number)\r\n<\/pre>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<p><span style=\"color: #000000;\">First Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">3<\/span><\/pre>\n<p><span style=\"color: #000000;\">Second Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">7<\/span><\/pre>\n<p><span style=\"color: #000000;\">Third Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">6<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><span style=\"color: #000000;\">Ex 2: random_number=random.randrange(10,21)<\/span><\/strong><\/p>\n<p><span style=\"color: #000000;\">The above code will generate random number between 10 to 20. (as we know indexing)<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import random\r\nrandom_number=random.randrange(10,21)\r\nprint(random_number)\r\n<\/pre>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<p><span style=\"color: #000000;\">First Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">11<\/span><\/pre>\n<p><span style=\"color: #000000;\">Second Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">18<\/span><\/pre>\n<p><span style=\"color: #000000;\">Third Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">16<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><span style=\"color: #000000;\">Ex 3: random_number=random.randrange(10,20,3)<\/span><\/strong><\/p>\n<p><span style=\"color: #000000;\">The above code will generate random number between 10 to 19 with step 3. (as we know indexing)<\/span><\/p>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">3. randint ()<\/span><\/h3>\n<p><span style=\"color: #000000;\">This function returns a random integer between a given start and stop integer.<\/span><\/p>\n<p><span style=\"color: #000000;\">Parameters: <\/span><span style=\"color: #000000;\">It takes two parameters. Both are mandatory.<\/span><\/p>\n<p><span style=\"color: #000000;\">start: It is the start position of a range. The default value is 0 if not specified.<\/span><br \/>\n<span style=\"color: #000000;\">stop: It is the end position of a range.<\/span><\/p>\n<p><strong><span style=\"color: #000000;\">Syntax:<\/span><\/strong><\/p>\n<p><strong><span style=\"color: #000000;\">random.randint(start,stop)<\/span><\/strong><\/p>\n<p><span style=\"color: #000000;\"><strong>Example:<\/strong><\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import random\r\nrandom_number=random.randint(10,20)\r\nprint(random_number)\r\n<\/pre>\n<p><span style=\"color: #000000;\"><strong>Output:<\/strong><\/span><\/p>\n<p><span style=\"color: #000000;\">First Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">18<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">4. uniform()<\/span><\/h3>\n<p><span style=\"color: #000000;\">Returns a random floating point number between 2 numbers.<\/span><\/p>\n<p><span style=\"color: #000000;\">Syntax:<\/span><\/p>\n<p><span style=\"color: #000000;\">random.uniform(a,b)<\/span><\/p>\n<p><span style=\"color: #000000;\">Here a is lower limit and b is the upper limit of the random float. The returned random float may be equal or greater than a and less than b.\u00a0<\/span><\/p>\n<p><span style=\"color: #000000;\">Example:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import random\r\nrandom_number=random.uniform(10,20)\r\nprint(random_number)\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<pre><span style=\"color: #000000;\">12.0785116829<\/span><\/pre>\n<p>&nbsp;<\/p>\n<h3><span style=\"color: #000000;\">5. choice()<\/span><\/h3>\n<p><span style=\"color: #000000;\">Used for making a random selection from a sequence (list,tuple,string)<\/span><\/p>\n<p><span style=\"color: #000000;\">Syntax:<\/span><\/p>\n<p><span style=\"color: #000000;\">random.choice(sequence)<\/span><\/p>\n<p><span style=\"color: #000000;\">Example:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import random\r\nmy_choice=random.choice([\"RED\",\"YELLOW\",\"GREEN\", \"BLUE\"])\r\nprint(my_choice)\r\n\r\n<\/pre>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<p><span style=\"color: #000000;\">First Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">BLUE<\/span><\/pre>\n<p><span style=\"color: #000000;\">Second Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">YELLOW<\/span><\/pre>\n<p><span style=\"color: #000000;\">Third Run:\u00a0<\/span><\/p>\n<pre><span style=\"color: #000000;\">GREEN<\/span><\/pre>\n<h3><span style=\"color: #000000;\">6. shuffle()<\/span><\/h3>\n<p><span style=\"color: #000000;\">Used to shuffle the contents in a sequence (list).<\/span><\/p>\n<p><span style=\"color: #000000;\">Syntax:<\/span><\/p>\n<p><span style=\"color: #000000;\">shuffle(list)<\/span><\/p>\n<p><span style=\"color: #000000;\">Example:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import random\r\ncolor=[\"RED\",\"YELLOW\",\"GREEN\", \"BLUE\"]\r\nrandom.shuffle(color)\r\nprint(color)<\/pre>\n<p><span style=\"color: #000000;\">Output:<\/span><\/p>\n<p><span style=\"color: #000000;\">First Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">['RED', 'GREEN', 'YELLOW', 'BLUE']<\/span><\/pre>\n<p><span style=\"color: #000000;\">Second Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">['GREEN', 'RED', 'YELLOW', 'BLUE']<\/span><\/pre>\n<p><span style=\"color: #000000;\">Third Run:<\/span><\/p>\n<pre><span style=\"color: #000000;\">['BLUE', 'YELLOW', 'RED', 'GREEN']<\/span><\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Random Module in Python Class 11-12 Notes The Random module in Python is an important topic in the Class 11 and Class 12 Computer Science syllabus and is widely used in practical programming. It provides built-in functions to generate random numbers, random choices, and random sequences, which are useful in games, simulations, and data-related programs. [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15,20],"tags":[],"class_list":["post-2623","post","type-post","status-publish","format-standard","hentry","category-cbse-sample-papers-class-11","category-cbse-computer-science-with-python-class-12"],"_links":{"self":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/2623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/comments?post=2623"}],"version-history":[{"count":1,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/2623\/revisions"}],"predecessor-version":[{"id":5577,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/posts\/2623\/revisions\/5577"}],"wp:attachment":[{"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/media?parent=2623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/categories?post=2623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cbsepython.in\/wp-json\/wp\/v2\/tags?post=2623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}