random.sample
时间: 2023-10-14 19:25:58 浏览: 39
The `random.sample` function is a method in the Python `random` module that returns a specified number of unique elements randomly chosen from a given sequence. This function takes two arguments: the first one is the sequence from which elements are to be chosen, and the second one is the number of unique elements that need to be chosen.
For example, the following code will return a list of three unique random integers between 1 and 10:
```python
import random
numbers = range(1, 11)
random_numbers = random.sample(numbers, 3)
print(random_numbers)
```
Output:
```
[8, 2, 5]
```
Note that the `random.sample` function will raise a `ValueError` if the specified number of unique elements is greater than the length of the sequence.
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)