파이썬의 `random` 모듈은 난수 생성 및 관련 기능을 다루는 데 사용됩니다. 아래에는 몇 가지 `random` 모듈 함수와 해당 예시 코드를 제공합니다.
1. `random.randint(a, b)`: a부터 b 사이의 정수 난수를 생성합니다.
```python
import random
random_number = random.randint(1, 10)
print(random_number) # 1부터 10까지의 임의의 정수
```
2. `random.random()`: 0.0과 1.0 사이의 부동 소수점 난수를 생성합니다.
```python
import random
random_float = random.random()
print(random_float) # 0.0부터 1.0 미만의 임의의 부동 소수점
```
3. `random.choice(sequence)`: 주어진 시퀀스에서 임의의 요소를 선택합니다.
```python
import random
fruits = ["사과", "바나나", "체리", "딸기"]
random_fruit = random.choice(fruits)
print(random_fruit) # fruits 중에서 임의의 과일 선택
```
4. `random.shuffle(sequence)`: 주어진 시퀀스를 섞습니다.
```python
import random
deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A']
random.shuffle(deck)
print(deck) # deck 내의 요소들을 섞은 결과
```
5. `random.sample(sequence, k)`: 주어진 시퀀스에서 중복되지 않는 k개의 요소를 무작위로 선택합니다.
```python
import random
lottery_numbers = list(range(1, 51))
winning_numbers = random.sample(lottery_numbers, 6)
print(winning_numbers) # 중복되지 않는 6개의 로또 번호
```
6. `random.uniform(a, b)`: a와 b 사이의 임의의 부동 소수점 난수를 생성합니다.
```python
import random
random_value = random.uniform(1.0, 2.0)
print(random_value) # 1.0과 2.0 사이의 임의의 부동 소수점
```
이러한 함수를 사용하여 다양한 난수 생성 및 관련 작업을 수행할 수 있습니다.