用python写一段代码,实现在1-20范围内随机抽五个数
时间: 2024-04-30 16:22:53 浏览: 20
以下是使用Python的random模块实现在1-20范围内随机抽五个数的代码:
```python
import random
# 生成1-20范围内的5个随机数
random_numbers = random.sample(range(1, 21), 5)
# 打印随机数列表
print(random_numbers)
```
运行结果可能如下:
```
[8, 2, 16, 3, 20]
```
相关问题
怎么用python在1到15范围内随机抽取6个数放列表里
可以使用Python内置的random模块中的sample函数来实现:
```python
import random
numbers = random.sample(range(1, 16), 6)
print(numbers)
```
这段代码会在1到15中随机抽取6个不重复的数字,然后将它们放在一个列表中并打印出来。
用numpy写一段随机生成60个1-2001之间的浮点数的代码
可以使用numpy库中的random模块生成随机数,如下所示:
```python
import numpy as np
# 生成60个1-2001之间的浮点数
arr = np.random.uniform(low=1, high=2001, size=60)
print(arr)
```
其中,`np.random.uniform()`函数可以生成指定范围内的随机浮点数,其中`low`参数表示最小值,`high`参数表示最大值,`size`参数表示生成的随机数个数。以上代码将生成一个包含60个1-2001之间的随机浮点数的一维numpy数组,并通过`print()`函数打印出来。