{"id":25494,"date":"2021-11-23T08:56:30","date_gmt":"2021-11-23T03:26:30","guid":{"rendered":"https:\/\/python-programs.com\/?p=25494"},"modified":"2021-11-23T08:56:30","modified_gmt":"2021-11-23T03:26:30","slug":"python-random-randrange-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-random-randrange-method-with-examples\/","title":{"rendered":"Python Random randrange() Method with Examples"},"content":{"rendered":"
Random randrange() Method in Python:<\/strong><\/p>\n The randrange() method selects an element at random from the specified range and returns it.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n start:<\/strong> This is Optional. It is an integer indicating the starting position. stop:<\/strong> This is Required. It is an integer indicating the position at which to end.<\/p>\n step:<\/strong> Optional. The incrementation is specified by an integer. 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 Explanation:<\/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 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 randrange() Method in Python: The randrange() method selects an element at random from the specified range and returns it. Syntax: random.randrange(start, stop, step) Parameters start: This is Optional. It is an integer indicating the starting position. 0 is the default. stop: This is Required. It is an integer indicating the position at which to …<\/p>\nrandom.randrange(start, stop, step)<\/pre>\n
\n0 is the default.<\/p>\n
\n1 is the default.<\/p>\nGiven start value = 1\r\nGiven stop value = 5<\/pre>\n
The random number between 1 and 5 = 3<\/pre>\n
Given start value = 1\r\nGiven stop value = 10\r\nGiven step size = 2<\/pre>\n
The random number between 1 and 10 = 7<\/pre>\n
Here it prints a random odd number between 1 and 10(includes only start value) \r\nsince the start value is 1 and stepsize is 2<\/pre>\n
Random randrange() 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(start value) as static input and store it in a variable.\r\ngvn_strtval = 1\r\n# Give the other number(stop value) as static input and store it in another variable.\r\ngvn_stopval = 5\r\n# Pass the given start and stop values as the arguments to the random.randrange()\r\n# method to get a random number between the given start and stop values.\r\n# (includes only start value)\r\n# Store it in another variable.\r\nrslt = random.randrange(gvn_strtval, gvn_stopval)\r\n# Print a random number between the given start and stop values.\r\nprint(\"The random number between\", gvn_strtval, \"and\", gvn_stopval, \"=\", rslt)\r\n<\/pre>\n
The random number between 1 and 5 = 3<\/pre>\n
with giving step value<\/h5>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the number(start value) as static input and store it in a variable.\r\ngvn_strtval = 1\r\n# Give the other number(stop value) as static input and store it in another variable.\r\ngvn_stopval = 10\r\n# Give the step size as static input and store it in another variable.\r\ngvn_step = 2\r\n# Pass the given start,stop and step values as the arguments to the\r\n# random.randrange() method to get a random number between the given start and\r\n# stop values(includes only start value) with a given stepsize.\r\n# Store it in another variable.\r\nrslt = random.randrange(gvn_strtval, gvn_stopval, gvn_step)\r\n# Print a random number between the given start and stop values.\r\nprint(\"The random number between\", gvn_strtval, \"and\", gvn_stopval, \"=\", rslt)\r\n<\/pre>\n
The random number between 1 and 10 = 7<\/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(start value) as user input using the int(input()) function and store it in a variable.\r\ngvn_strtval = int(input(\"Enter some random number = \"))\r\n# Give the other number(stop value) \u00a0as user input using the int(input()) function \r\n# and store it in another variable.\r\ngvn_stopval = int(input(\"Enter some random number = \"))\r\n# Pass the given start and stop values as the arguments to the random.randrange()\r\n# method to get a random number between the given start and stop values.\r\n# (includes only start value)\r\n# Store it in another variable.\r\nrslt = random.randrange(gvn_strtval, gvn_stopval)\r\n# Print a random number between the given start and stop values.\r\nprint(\"The random number between\", gvn_strtval, \"and\", gvn_stopval, \"=\", rslt)\r\n<\/pre>\n
Enter some random number = 10\r\nEnter some random number = 17\r\nThe random number between 10 and 17 = 10<\/pre>\n
with giving step value<\/h5>\n
\n
# Import random module using the import keyword.\r\nimport random\r\n# Give the number(start value) as user input using the int(input()) function and store it in a variable.\r\ngvn_strtval = int(input(\"Enter some random number = \"))\r\n# Give the other number(stop value) as user input using the int(input()) function \r\n# and store it in another variable.\r\ngvn_stopval = int(input(\"Enter some random number = \"))\r\n# Give the step size as user input using the int(input()) function and store it in another variable.\r\ngvn_step = int(input(\"Enter some random number = \"))\r\n# Pass the given start,stop and step values as the arguments to the\r\n# random.randrange() method to get a random number between the given start and\r\n# stop values(includes only start value) with a given stepsize.\r\n# Store it in another variable.\r\nrslt = random.randrange(gvn_strtval, gvn_stopval, gvn_step)\r\n# Print a random number between the given start and stop values.\r\nprint(\"The random number between\", gvn_strtval, \"and\", gvn_stopval, \"=\", rslt)\r\n<\/pre>\n
Enter some random number = 50\r\nEnter some random number = 60\r\nEnter some random number = 3\r\nThe random number between 50 and 60 = 53<\/pre>\n","protected":false},"excerpt":{"rendered":"