译(三十四)-Python获取随机整数
如有翻译问题欢迎评论指出,谢谢。
垂死病中惊坐起,忽闻今天是周六。
生成属于[0, 9]的随机数
aneuryzm asked:
- 怎么在Python中生成0到9(包括0与9)的随机整数
- 例如
0
,1
,2
,3
,4
,5
,6
,7
,8
,9
Answers:
kovshenin - vote: 2344
试试:
from random import randrange print(randrange(10))
文档: https://docs.python.org/3/library/random.html#random.randrange
JMSamudio - vote: 637
import random print(random.randint(0,9))
random.randint(a, b)
随机返回整数N, N∈[a, b]
文档: https://docs.python.org/3.1/library/random.html#random.randint
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
aneuryzm asked:
- 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
- How can I generate random integers between 0 and 9 (inclusive) in Python?
Answers:
kovshenin - vote: 2344
Try:
试试:from random import randrange print(randrange(10))
Docs: https://docs.python.org/3/library/random.html#random.randrange
文档: https://docs.python.org/3/library/random.html#random.randrangeJMSamudio - 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]Docs: https://docs.python.org/3.1/library/random.html#random.randint
文档: https://docs.python.org/3.1/library/random.html#random.randintAndrew 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)
共有 0 条评论