Python3 randrange() Function

Python3 数字Python3 Numbers


Description

randrange()The method returns a random number from a set with the specified increment base, with the base defaulting to 1.


Syntax

The following is the syntax for the randrange() method:

import random

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

Note:randrange() is not directly accessible; you need to import the random module and then call the method using the random static object.


Parameters

  • start -- The starting value of the specified range, included in the range.
  • stop -- The ending value of the specified range, not included in the range.
  • step -- The specified increment.

Return Value

Returns a random item from the given range.

Examples

The following example demonstrates the use of the randrange() method:

Example (Python 3.0+)

#!/usr/bin/python3 #!/usr/bin/python3 import random # Select an odd number from 1-100 print ("randrange(1,100, 2) : ", random.randrange(1, 100, 2)) # Select a random number from 0-99 print ("randrange(100) : ", random.randrange(100))

The output of the above example after running is:

randrange(1,100, 2) :  97
randrange(100) :  42

Python3 数字Python3 Numbers

Other Extensions