본문으로 바로가기

랜덤 함수

category onYouTube/Python 2021. 3. 13. 22:56
  • random( ) : 0.0 ~ 1.0미만의 임의의 값 생성
  • random( ) * 10 : 0.0 ~ 10.0미만의 임의의 값 생성
  • int(random( ) * 10) : 0 ~ 10미만의 임의의 정수값 생성
  • int(random( ) * 10) + 1 : 1 ~ 10이하의 임의의 정수값 생성

 

  • irandrange(a, b) : a ~ b미만의 임의의 정수값 생성

 

  • randint(a, b) : a ~ b이하의 임의의 정수값 생성

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 ~ 45이하의 임의의 정수값 생성

print(randrange(1, 46)) # 1 ~ 46미만의 임의의 정수값 생성

print(randint(1, 45)) # 1 ~ 45이하의 임의의 정수값 생성

 

 

-출처-

https://www.youtube.com/watch?v=kWiCuklohdY

'onYouTube > Python' 카테고리의 다른 글

리스트  (0) 2021.03.13
탈출 문자  (0) 2021.03.13
문자열 포맷  (0) 2021.03.13
문자열 처리 함수  (0) 2021.03.13
숫자 처리 함수  (0) 2021.03.13