{"id":25512,"date":"2021-11-19T09:12:52","date_gmt":"2021-11-19T03:42:52","guid":{"rendered":"https:\/\/python-programs.com\/?p=25512"},"modified":"2021-11-19T09:12:52","modified_gmt":"2021-11-19T03:42:52","slug":"python-random-uniform-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-random-uniform-method-with-examples\/","title":{"rendered":"Python Random uniform() Method with Examples"},"content":{"rendered":"
Random uniform() Method in Python:<\/strong><\/p>\n The uniform() method generates a random floating-point number between the two input values (both included).<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n a:<\/strong> This is Required. A number indicating the lowest possible outcome.<\/p>\n b:<\/strong> This is Required. A number indicating the highest possible outcome.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Random uniform() Method in Python: The uniform() method generates a random floating-point number between the two input values (both included). Syntax: random.uniform(a, b) Parameters a: This is Required. A number indicating the lowest possible outcome. b: This is Required. A number indicating the highest possible outcome. Examples: Example1: Input: Given first number = 10 Given …<\/p>\nrandom.uniform(a, b)<\/pre>\n
Given first number = 10\r\nGiven second number = 20<\/pre>\n
The random floating-point number between 10 and 20 = 17.597119991005258\r\n(both 10 and 20 included)<\/pre>\n
Given first number = 80\r\nGiven second number = 90<\/pre>\n
The random floating-point number between 80 and 90 = 81.63985884463067\r\n(both 80 and 90 included)<\/pre>\n
Random uniform() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the number as static input and store it in a variable.\r\ngvn_fstnum = 10\r\n# Give the other number as static input and store it in another variable.\r\ngvn_scndnum = 20\r\n# Pass the given two numbers as the arguments to the random.uniform() method\r\n# that generates a random floating-point number between the given two numbers\r\n# (both included).\r\n# Store it in another variable.\r\nrslt = random.uniform(gvn_fstnum, gvn_scndnum)\r\n# Print a random floating-point number between the two numbers (both included).\r\nprint(\"The random floating-point number between\",\r\n gvn_fstnum, \"and\", gvn_scndnum, \"=\", rslt)\r\n<\/pre>\n
The random floating-point number between 10 and 20 = 17.597119991005258<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the number as user input using the int(input()) function and store it in a variable.\r\ngvn_fstnum = int(input(\"Enter some random number = \"))\r\n# Give the other number as user input using the int(input()) function \r\n# and store it in another variable.\r\ngvn_scndnum = int(input(\"Enter some random number = \"))\r\n\r\n# Pass the given two numbers as the arguments to the random.uniform() method\r\n# that generates a random floating-point number between the given two numbers\r\n# (both included).\r\n# Store it in another variable.\r\nrslt = random.uniform(gvn_fstnum, gvn_scndnum)\r\n# Print a random floating-point number between the two numbers (both included).\r\nprint(\"The random floating-point number between\",\r\n gvn_fstnum, \"and\", gvn_scndnum, \"=\", rslt)\r\n<\/pre>\n
Enter some random number = 80\r\nEnter some random number = 90\r\nThe random floating-point number between 80 and 90 = 81.63985884463067<\/pre>\n","protected":false},"excerpt":{"rendered":"