{"id":3566,"date":"2023-10-22T09:53:12","date_gmt":"2023-10-22T04:23:12","guid":{"rendered":"https:\/\/python-programs.com\/?p=3566"},"modified":"2023-11-10T11:53:54","modified_gmt":"2023-11-10T06:23:54","slug":"python-programming-random-module","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-programming-random-module\/","title":{"rendered":"Python Programming \u2013 Random module"},"content":{"rendered":"

In this Page, We are Providing Python Programming \u2013 Random module. Students can visit for more Detail and Explanation of Python Handwritten Notes<\/a>\u00a0Pdf.<\/p>\n

Python Programming \u2013 Random module<\/h2>\n

Random module<\/strong><\/p>\n

This module implements pseudo-random number generators for various distributions. Almost all module functions depend on the basic function random ( ), which generates a random float uniformly in the semi-open range [0 . 0 , 1 . 0 ). Python uses the “Mersenne Twister” as the core generator. However, Mersenne Twister being completely deterministic, it is not suitable for all purposes and is completely unsuitable for cryptographic purposes.<\/p>\n

Functions for integers<\/strong><\/p>\n

random.randrange ( stop )<\/p>\n

Return a randomly selected integer element from range ( 0 , stop ) .<\/p>\n

>>> random . randrange ( 88 )\r\n17\r\n>>> random . randrange ( -88 )\r\n\r\nTraceback ( most recent call last ) :\r\nFile \"<pyshell#l>\" , line 1 , in <module> \r\nrandom . randrange ( -88 )\r\nFile \" C : \\ Python27 \\ lib \\ random . py \" , line 191 , in randrange \r\nraise ValueError , \" empty range for randrange ( ) \"\r\nValueError: empty range for randrange ( )<\/pre>\n

random . randrange ( start , stop [ , step ] )
\nReturn a randomly selected integer element from range ( start , stop , step ) .<\/p>\n

>>> random . randrange ( 3 , 100 , 5 )\r\n83<\/pre>\n

random . randint ( a , b )
\nReturn a random integer N such that a<=N<=b.<\/p>\n

>>> random . randint ( 5 , 86 )\r\n70<\/pre>\n

Functions for sequences<\/strong><\/p>\n

random . choice ( seq )
\nReturn a random element from the non-empty sequence seq. If seq is empty, raises IndexError.<\/p>\n

>>> random . choice ( ' abcdefghij ' )\r\n' e ' \r\n>>> random . choice ( [ ' aa ' , ' bb ' , ' cc ' , 11 , 22 ] )\r\n' cc '<\/pre>\n

random . shuffle ( x [ , random ] )
\nShuffle the sequence x in place. The optional argument random is a O-argument function returning a random float in [ 0 . 0 , 1 . 0 ); by default, this is the function random ( ).<\/p>\n

>>> items= [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ]\r\n>>> random . shuffle ( items )\r\n>>> items\r\n[ 4 , 7 , 2 , 6 , 3 , 5 , 1 ]<\/pre>\n

random . sample ( population , k )
\nReturn a k length list of unique elements chosen from the population sequence; used for random sampling without replacement. Return a new list containing elements from the population, while leaving the original population unchanged. If the population contain repeating elements, then each occurrence is a possible selection in the sample.<\/p>\n

>>> random . sample ( [ 1 , 2 , 3 , 4 , 5 ] , 3 )\r\n[ 4 , 5 , 1 ]<\/pre>\n

To choose a sample from a range of integers, use an xrange ( ) object as an argument. This is especially fast and space efficient for sampling from a large population.<\/p>\n

>>> random . sample ( xrange ( 10000000 ) , 5 )\r\n[ 2445367 , 2052603 , 975267 , 3021085 , 6098369 ]<\/pre>\n

Functions for floating point values<\/strong><\/p>\n

random . random ( )
\nReturn the next random floating point number in the range [ 0 . 0 , 1 . 0 ).<\/p>\n

>>> random . random ( )\r\n0.6229016948897019<\/pre>\n

random . uniform ( a , b )
\nReturn a random floating point number N, such that a<=N<=b for a<=b and b<=N<=a for b<a.<\/p>\n

>>> random . uniform ( 0 . 5 , 0 . 6 )\r\n0.5795193565565696<\/pre>\n

random . triangular ( low , high , mode )
\nReturn a random floating point number N such that low<=N<=high and with the specified mode between those bounds. The low and high bounds default to 0 and 1, respectively. The mode argument defaults to the midpoint between the bounds, giving a symmetric distribution.<\/p>\n

>>> random . triangular ( 2 . 8 , 10 . 9 , 7 . 5 )\r\n6.676127015045406<\/pre>\n

random . betavariate ( alpha , beta )
\nBeta distribution; conditions of the parameters are alpha>0 and beta>0. Returned values range between 0 and 1.<\/p>\n

>>> random . betavariate ( 2 . 5 , 1 . 0 )\r\n0.543590525336106<\/pre>\n

random . expovariate ( lambd )
\nExponential distribution; lambd is 1.0 divided by the desired mean. It should be nonzero. Returned values range from 0 to positive infinity; if lambd is positive, and from negative infinity to 0, if lambd is negative.<\/p>\n

>>> random . expovariate ( 0 . 5 )\r\n1.5287594548764503<\/pre>\n

random . gammavariate ( alpha , beta )
\nGamma distribution (not the gamma function). Conditions of the parameters are alpha>0 and beta>0.<\/p>\n

>>> random . gammavariate ( 1 . 3 , 0 . 5 )\r\n0.5893587279305473<\/pre>\n

random . gauss ( mu , sigma )
\nGaussian distribution; mu is the mean, and sigma is the standard deviation. This is slightly faster than the normalvariate () function defined below.<\/p>\n

>>> random . gauss ( 0 . 5 , 1 . 9 )\r\n-1.8886943114939512<\/pre>\n

random . lognormvariate ( mu , sigma )
\nLog normal distribution; if natural logarithm of this distribution is taken, a normal distribution with mean mu, and standard deviation sigma is received, mu can have any value, and sigma must be greater than zero.<\/p>\n

>>> random . lognormvariate ( 0 . 5 , 1 . 9 )\r\n4.621063728160664<\/pre>\n

random . normalvariate ( mu , sigma )
\nNormal distribution; mu is the mean, and sigma is the standard deviation.<\/p>\n

>>> random . normalvariate ( 0 . 5 , 1 . 9 )\r\n1.6246107732503214<\/pre>\n

random . vonmisesvariate ( mu , kappa )
\nmu is the mean angle, expressed in radians between 0 and 271, and kappa is the concentration parameter, which must be greater than or equal to zero. If kappa is equal to zero, this distribution reduces to a uniform random angle over the range 0 to 2K .<\/p>\n

>>> random . vonmisesvariate ( 0 . 5 , 1 . 9 )\r\n-0.4664831190641767<\/pre>\n

random . paretovariate ( alpha )
\nPareto distribution; alpha is the shape parameter.<\/p>\n

>>> random . paretovariate ( 0 . 5 )\r\n60.471412103322585<\/pre>\n

random . weibullvariate ( alpha , beta )
\nWeibull distribution; alpha is the scale parameter and beta is the shape parameter.<\/p>\n

>>> random . weibullvariate ( 0 . 5 , 1 . 9 )\r\n0.9229896561284915<\/pre>\n

Alternative generators<\/strong><\/p>\n

Apart from Mersenne Twister, there are more core random number generator, such as generator based on “Wichmann-Hill” algorithm.<\/p>\n

>>> rnd=random . WichmannHill ( )\r\n>>> rnd . random ( )\r\n0.4065226158909223\r\n>>>\r\n>>> rnd=random . SystemRandom ( )\r\n>>> rnd . random ( )\r\n0.46579102190832355<\/pre>\n","protected":false},"excerpt":{"rendered":"

In this Page, We are Providing Python Programming \u2013 Random module. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Random module Random module This module implements pseudo-random number generators for various distributions. Almost all module functions depend on the basic function random ( ), which generates a random …<\/p>\n

Python Programming \u2013 Random module<\/span> Read More »<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython Programming \u2013 Random module - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-programming-random-module\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python Programming \u2013 Random module - Python Programs\" \/>\n<meta property=\"og:description\" content=\"In this Page, We are Providing Python Programming \u2013 Random module. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Random module Random module This module implements pseudo-random number generators for various distributions. Almost all module functions depend on the basic function random ( ), which generates a random … Python Programming \u2013 Random module Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-programming-random-module\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-22T04:23:12+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-10T06:23:54+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Prasanna\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-programming-random-module\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-programming-random-module\/\",\"name\":\"Python Programming \u2013 Random module - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2023-10-22T04:23:12+00:00\",\"dateModified\":\"2023-11-10T06:23:54+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-random-module\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-programming-random-module\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-programming-random-module\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python Programming \u2013 Random module\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-programming-random-module\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-random-module\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\"},\"headline\":\"Python Programming \u2013 Random module\",\"datePublished\":\"2023-10-22T04:23:12+00:00\",\"dateModified\":\"2023-11-10T06:23:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-programming-random-module\/#webpage\"},\"wordCount\":624,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-programming-random-module\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb\",\"name\":\"Prasanna\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g\",\"caption\":\"Prasanna\"},\"url\":\"https:\/\/python-programs.com\/author\/prasanna\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python Programming \u2013 Random module - Python Programs","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:\/\/python-programs.com\/python-programming-random-module\/","og_locale":"en_US","og_type":"article","og_title":"Python Programming \u2013 Random module - Python Programs","og_description":"In this Page, We are Providing Python Programming \u2013 Random module. Students can visit for more Detail and Explanation of Python Handwritten Notes\u00a0Pdf. Python Programming \u2013 Random module Random module This module implements pseudo-random number generators for various distributions. Almost all module functions depend on the basic function random ( ), which generates a random … Python Programming \u2013 Random module Read More »","og_url":"https:\/\/python-programs.com\/python-programming-random-module\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2023-10-22T04:23:12+00:00","article_modified_time":"2023-11-10T06:23:54+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Prasanna","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-programming-random-module\/#webpage","url":"https:\/\/python-programs.com\/python-programming-random-module\/","name":"Python Programming \u2013 Random module - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2023-10-22T04:23:12+00:00","dateModified":"2023-11-10T06:23:54+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-programming-random-module\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-programming-random-module\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-programming-random-module\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python Programming \u2013 Random module"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-programming-random-module\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-programming-random-module\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb"},"headline":"Python Programming \u2013 Random module","datePublished":"2023-10-22T04:23:12+00:00","dateModified":"2023-11-10T06:23:54+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-programming-random-module\/#webpage"},"wordCount":624,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-programming-random-module\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/ea8ce699662f0d7e248e52fe080b85bb","name":"Prasanna","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/174540ad43736c7d1a4c4f83c775e74d?s=96&d=mm&r=g","caption":"Prasanna"},"url":"https:\/\/python-programs.com\/author\/prasanna\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3566"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=3566"}],"version-history":[{"count":1,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3566\/revisions"}],"predecessor-version":[{"id":3567,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/3566\/revisions\/3567"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=3566"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=3566"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=3566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}