后浪云Python教程:python怎么产生5个随机数字?

后浪云Python教程:python怎么产生5个随机数字?插图

python生成5个随机数的方法:

1、在循环中使用random.randint()方法生成5个随机数

import random
count = 0
while (count < 5):
    print(random.randint(0,9))
    count = count + 1

输出结果如下:

9
4
7
3
8

random.randint:

语法:

random.randint(a,b)

函数返回数字 N ,N 为 a 到 b 之间的数字(a <= N <= b),包含 a 和 b。

THE END