Python

[Python/파이썬] Random

wowstudylog 2024. 12. 2. 14:20
반응형

 

 

 

 

Random


random: 난수를 발생시키는 모듈

 

 

# random()

import random
rn = random.random()
print(rn)
>>> 0.49439286446777775

random()함수는 0이상 1미만의 숫자를 랜덤으로 추출해주는 함수이다

 

 

# randint()

imoprt random
rn = random.randint(1,6)
print(rn)
>>> 6

randint()함수를 이용해 1부터 6까지의 정수 중 하나를 랜덤으로 추출할 수 있다

 

 

# shuffle()

import random
num = [1, 2, 3, 4, 5]
random.shuffle(num)
print(num)
>>> [3, 1, 4, 2, 5]

shuffle()함수는 자료를 무작위로 섞어주는 함수이다

 

 

# choice()

import random
num = [1, 2, 3, 4, 5]
a = random.choice(num)
print(a)
>>> 5

choice()함수를 이용해 리스트에서 무작위로 하나를 선택해 출력할 수 있다

반응형