Latest web development tutorials

Python3 randrange () function

Python3 digital Python3 digital


description

randrange () method returns the specified increments cardinality of a random number, the base defaults to 1.


grammar

The following is the syntax 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 by random static objects.


parameter

  • start - start value within a specified range, it included in the scope.
  • stop - end value within a specified range, not included in the scope.
  • step - increment specified base.

return value

Returns a random item from the given range.

Examples

The following shows an example of using randrange () method:

#!/usr/bin/python3
import random

# 从 1-100 中选取一个奇数
print ("randrange(1,100, 2) : ", random.randrange(1, 100, 2))

# 从 0-99 选取一个随机数
print ("randrange(100) : ", random.randrange(100))

After running the above example output is:

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

Python3 digital Python3 digital