데이터 분석/파이썬

파이썬 - random 모듈과 time 모듈

김각도 2022. 11. 16. 19:11
반응형

random 모듈

 

# n1 이상 n2 미만의 난수를 반환

import random

random.randrange(n1, n2)

 

# n1 이상 n2 이하의 랜덤 정수를 반환

import random

random.randint(1, 5)

 

# 리스트에 나열된 데이터 중 랜덤으로 n개를 선택하여 반환

import random

random.sample([1, 2, 3, 4, 5], n)

 

# 리스트에 나열된 데이터의 순서 섞기

import random

abc = [‘a’, ‘b’, ‘c’, ‘d’]

random.shuffle(abc)

 

# 리스트의 데이터 중 랜덤으로 하나를 반환

import random

abc = [‘a’, ‘b’, ‘c’, ‘d’]

random.choice(abc)

 

 

time 모듈

import time

lt = time.localtime()

print(f’time.localtime(): {lt}’)      # 웬만한 시간 정보를 다 반환

print(f’lt.tm_year: {lt.tm_year})     # year 대신 mon, mday, hour, wday 등 사용 가능!

 

반응형