zl程序教程

您现在的位置是:首页 >  Python

当前栏目

python怎么产生5个随机数字?

2023-03-20 14:45:17 时间

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。

更多Python知识请关注Python自学网。