# 译（三十四）-Python获取随机整数 stackoverflow热门问题目录

#### 生成属于[0, 9]的随机数

• 怎么在Python中生成0到9（包括0与9）的随机整数
• 例如 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

• kovshenin – vote: 2344

• 试试：

• from random import randrange
print(randrange(10))
• JMSamudio – vote: 637

• import random
print(random.randint(0,9))
• random.randint(a, b)
• 随机返回整数N, N∈[a, b]

• Andrew Hare – vote: 159

• 试试这个

• 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)

#### Generate random integers between 0 and 9

• How can I generate random integers between 0 and 9 (inclusive) in Python?
怎么在Python中生成0到9（包括0与9）的随机整数
• For example, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
例如 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

• kovshenin – vote: 2344

• Try:
试试：

• from random import randrange
print(randrange(10))
• JMSamudio – vote: 637

• import random
print(random.randint(0,9))
• random.randint(a, b)
• Return a random integer N such that a <= N <= b.
随机返回整数N, N∈[a, b]

• Andrew Hare – vote: 159

• Try this:
试试这个

• 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)