Generate random integers between 0 and 9

How can I generate random integers between 0 and 9 (inclusive) in Python?

ie 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9


Try:

from random import randint
print(randint(0, 9))

More info: https://docs.python.org/3/library/random.html#random.randint


import random
print(random.randint(0,9))

random.randint(a, b)

Return a random integer N such that a <= N <= b.

Docs: 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/2924.html

上一篇: 在JavaScript中生成两个数字之间的随机数

下一篇: 生成0到9之间的随机整数