用python随机生成一个整数
时间: 2024-05-09 07:16:42 浏览: 108
可以使用Python的内置模块`random`来生成一个随机整数。下面是一个简单的示例代码:
```python
import random
# 生成1到10之间的随机整数(包括1和10)
random_int = random.randint(1, 10)
print(random_int)
```
输出:
```
7
```
其中,`randint(a, b)`函数用于生成一个范围在`a`和`b`之间(包括`a`和`b`)的随机整数。
相关问题
如何用Python随机生成十个整数
可以使用random模块中的randint方法生成随机整数,结合循环生成十个整数。
```python
import random
for i in range(10):
num = random.randint(1, 100)
print(num)
```
以上代码会生成10个1到100之间的随机整数,并依次输出到控制台。可以根据需要修改randint的参数来生成不同范围的整数。
python 随机生成十个整数
以下是 Python 代码,用于随机生成十个整数:
```python
import random
# 生成十个 0 到 100 的整数
numbers = [random.randint(0, 100) for _ in range(10)]
print(numbers)
```
输出结果可能如下:
```
[45, 89, 23, 7, 56, 78, 12, 34, 88, 99]
```
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)