在Python中生成0到9之间的随机整数
以下是一些使用不同的python模块的解释性说明,它们如何生成随机整数?考虑生成介于0和9之间(包括两端)的随机数的方案。
使用randrange
语法:
random.randrange(stop)
random.randrange(start, stop, step)代码:
>>> import random >>> for i in range(10): ... print(random.randrange(10)) ... 2 2 2 0 8 8 5 6 6 3
使用randint
语法:
random.randint(a,b)
代码:
>>> import random >>> for i in range(10): ... print(random.randint(0,10)) ... 1 6 7 5 8 9 6 2 3 9 >>>
使用secrets
通过使用此方法,我们可以生成加密强度高的随机数。
>>> from secrets import randbelow >>> for i in range(10): ... print(randbelow(10)) ... 6 5 2 0 7 2 0 1 2 6 >>>
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短