{"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

random.randrange(start, stop, step)<\/pre>\n

Parameters<\/strong><\/p>\n

start:<\/strong> This is Optional. It is an integer indicating the starting position.
\n0 is the default.<\/p>\n

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.
\n1 is the default.<\/p>\n

Examples:<\/strong><\/p>\n

Example1:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given start value = 1\r\nGiven stop value = 5<\/pre>\n

Output:<\/strong><\/p>\n

The random number between 1 and 5 = 3<\/pre>\n

Example2:<\/strong><\/p>\n

Input:<\/strong><\/p>\n

Given start value = 1\r\nGiven stop value = 10\r\nGiven step size = 2<\/pre>\n

Output:<\/strong><\/p>\n

The random number between 1 and 10 = 7<\/pre>\n

Explanation:<\/strong><\/p>\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