[Python] 랜덤 함수

룽지·2021년 8월 21일
0

Python

목록 보기
4/10
post-thumbnail

1. 랜덤 함수

  • 랜덤 함수 라이브러리 선언
    - from random import *

  • random()
    : 0.0 ~ 1.0 미만의 임의의 값 생성

    • 숫자 범위 늘릴 경우 : random() * 숫자
    • 정수의 임의의 값 생성 : int(random() * 숫자)
  • randrange(1, 46)
    : 1 ~ 46 미만의 임의의 값 생성

  • randint(1, 45)
    : 1 ~ 45 이하의 임의의 값 생성

from random import *

print(random()) #0.0 ~ 1.0 미만의 임의의 값 생성
print(random() * 10) #0.0 ~ 10.0 미만의 임의의 값 생성
print(int(random() * 10)) #0 ~ 10 미만의 임의의 값 생성
print(int(random() * 10) + 1) #1 ~ 10 이하의 임의의 값 생성
print(int(random() * 45) + 1) #1 ~ 46 미만의 임의의 값 생성

print(randrange(1, 46)) #1 ~ 46 미만의 임의의 값 생성
print(randint(1, 45)) #1 ~ 45 이하의 임의의 값 생성

>>>
0.17636233709059135
1.0306318651977253
1
1
23
29
19



📒 참고 자료
https://www.youtube.com/watch?v=kWiCuklohdY

0개의 댓글