生成0到9之间的随机整数
我如何在Python中生成0到9(含)之间的随机整数?
即0
, 1
, 2
, 3
, 4
, 5
, 6
, 7
, 8
, 9
尝试:
from random import randint
print(randint(0, 9))
更多信息:https://docs.python.org/3/library/random.html#random.randint
import random
print(random.randint(0,9))
random.randint(a, b)
返回一个随机整数N,使得a <= N <= b。
文档:https://docs.python.org/3.1/library/random.html#random.randint
尝试这个:
from random import randrange, uniform
# randrange gives you an integral value
irand = randrange(0, 10)
# uniform gives you a floating-point value
frand = uniform(0, 10)
链接地址: http://www.djcxy.com/p/2923.html