Python Number randrange() Method


Advertisements

Description

Python number method randrange() returns a randomly selected element from range(start, stop, step).

Syntax

Following is the syntax for randrange() method −

randrange ([start,] stop [,step])

Note − This function is not accessible directly, so we need to import random module and then we need to call this function using random static object.

Parameters

  • start − Start point of the range. This would be included in the range.

  • stop − Stop point of the range. This would be excluded from the range.

  • step − Steps to be added in a number to decide a random number.

Return Value

This method returns a random item from the given range

Example

The following example shows the usage of randrange() method.

#!/usr/bin/python
import random

# Select an even number in 100 <= number < 1000
print "randrange(100, 1000, 2) : ", random.randrange(100, 1000, 2)

# Select another number in 100 <= number < 1000
print "randrange(100, 1000, 3) : ", random.randrange(100, 1000, 3)

When we run above program, it produces following result −

randrange(100, 1000, 2) :  976
randrange(100, 1000, 3) :  520
python_numbers.htm

Useful Video Courses


Video

Python Online Training

Most Popular

187 Lectures 17.5 hours

Malhar Lathkar

Video

Python Essentials Online Training

55 Lectures 8 hours

Arnab Chakraborty

Video

Learn Python Programming in 100 Easy Steps

136 Lectures 11 hours

In28Minutes Official

Video

Python with Data Science

75 Lectures 13 hours

Eduonix Learning Solutions

Video

Python 3 from scratch to become a developer in demand

70 Lectures 8.5 hours

Lets Kode It

Video

Python Data Science basics with Numpy, Pandas and Matplotlib

63 Lectures 6 hours

Abhilash Nelson

Advertisements