Bootstrap

randint用法 python_Python randint()用法及代碼示例

randint()是Python3中隨機模塊的內置函數。隨機模塊提供對各種有用功能的訪問,其中一個功能可以生成隨機數,即randint()。句法:

randint(start, end)

參數:

(start, end): Both of them must be integer type values.

返回值:

A random integer within the given range as parameters.

錯誤和異常:

ValueError:Returns a ValueError when floating

point values are passed as parameters.

TypeError:Returns a TypeError when anything other than

numeric values are passed as parameters.

代碼1:

# Python3 program explaining work

# of randint() function

# imports random module

import random

# Generates a random number between

# a given positive range

r1 = random.randint(0, 10)

print("Random number between 0 and 10 is % s" % (r1))

# Generates a random number between

# two given negative range

<
;