Python/Python Basic

파이썬 Basic : 랜덤 함수

꾸준하게 :) 2020. 3. 7. 15:16
# 랜덤함수
from random import *
print(random()) # 0.0 이상 1.0 미만의 임의의 값 생성
print(random() * 10) # 0.0 이상 10.0 미만의 임의의 값 생성
print()
# 0 이상 10 미만의 임의의 값 생성
print(int(random() * 10))
print()
# 1 이상 10 이하의 임의의 값 생성
print(int(random() * 10) + 1)
print()
# 1 이상 45 이하의 임의의 값 생성
print(int(random() * 45) + 1)
print(int(random() * 45) + 1)
print(int(random() * 45) + 1)
print(int(random() * 45) + 1)
print(int(random() * 45) + 1)
print(int(random() * 45) + 1)
print()
# 1 이상 46 미만의 임의의 값 생성
print(randrange(1, 46))
print(randrange(1, 46))
print(randrange(1, 46))
print(randrange(1, 46))
print(randrange(1, 46))
print(randrange(1, 46))
print()
# 1 이상 45 이하의 임의의 값 생성
print(randint(1, 45))
print(randint(1, 45))
print(randint(1, 45))
print(randint(1, 45))
print(randint(1, 45))
print(randint(1, 45))
view raw .py hosted with ❤ by GitHub

 

[1] random() : 0.0 이상 1.0 미만의 임의의 값을 생성합니다.

 

[2] int(random()) : int로 랜덤 함수를 감싸게 되면 정수형으로 출력할 수 있습니다.

 

[3] randrange(A, B) : A 이상 B 미만의 임의의 값을 생성합니다.

 

[4] randint(A, B) : A 이상 B 이하의 임의의 값을 생성합니다.

 

 

 

 

출처 : https://www.youtube.com/watch?v=kWiCuklohdY&t=878s