python random
时间: 2023-10-07 12:11:24 浏览: 97
python随机模块random的22种函数(小结)
The `random` module in Python is used to generate random numbers. It provides various functions for generating random numbers, random sequences, and random choices. Some of the commonly used functions in the `random` module are:
1. `random.random()` - returns a random float between 0 and 1.
2. `random.randint(a, b)` - returns a random integer between a and b (inclusive).
3. `random.choice(sequence)` - returns a random element from the given sequence.
4. `random.shuffle(sequence)` - shuffles the elements in the given sequence randomly.
5. `random.sample(sequence, k)` - returns k random elements from the given sequence.
Here's an example of how to use the `random` module to generate a random number between 1 and 10:
```
import random
num = random.randint(1, 10)
print(num)
```
Output:
```
7
```
阅读全文