[๐ปPython] pearl's python ๋ณ์๋ฆฌ ํ์ถ๊ธฐ ๐ฃ
[Python]random ๋ชจ๋
seapearl
2024. 5. 13. 17:12
- ๋๋ค ๋ชจ๋์ ์์์ ์๋ฅผ ์์ฑํ๊ฑฐ๋, ๋ฆฌ์คํธ ๋ด ์์๋ฅผ ๋ฌด์์์ ์ผ๋ก ์๊ฑฐ๋ ์ ํํ๋ ํจ์๋ฅผ ํฌํจํ๊ณ ์๋ ๋ชจ๋

random() :0์์ 1์ฌ์ด์ ์ค์๋ฅผ ์์ฑํ๋ค. (1์ ํฌํจ x)
import random as rd # random๋ชจ๋ ๋ถ๋ฌ์ค๊ธฐ
rd.random()
#0.12345553456 (์์์ ์ค์๊ฐ์ด ๋์ด)
randrange() : ์ง์ ๋ ๋ฒ์ ๋ด์ ์ ์๋ฅผ ๋ฐํ
rd.randrange(1, 7) # 1์ด์7๋ฏธ๋ง์์ ์๋ฅผ๋ฐํ
rd.randrange(0, 10, 2) #1์ด์10๋ฏธ๋ง์ ์์ค2์๋ฐฐ์๋ฅผ๋ฐํ
randint(a,b) : a<=n<=b ์ฌ์ด์ ๋๋คํ ๊ฐ n์ ๋ฐํ
rd.randint(1, 10) # 1์ด์10์ดํ์(1, 10์ดํฌํจ)์์์์ ์๋ฅผ๋ฐํ
shuffle(seq) : ์ฃผ์ด์ง seq ๋ฆฌ์คํธ์ ์์๋ฅผ ๋๋คํ๊ฒ ์๋๋ค.
numlist = [10, 20, 30, 40, 50]
rd.shuffle(numlist)
numlist
#[20, 30, 10, 40, 50]
choice() ์ธ์๋ก ๋ค์ด์จ ์ํ์ค๋ก๋ถํฐ ์์์ ์์๋ฅผ ์ถ์ถ
rd.choice(numlist)
sample() ์์๋ฅผ ๋๋คํ๊ฒ ๋ฐํ , ๋ฐํํ ์์์ ๊ฐ์๋ฅผ ์ธ์๋ก ๋ฃ์ด์ค์ ์์
rd.sample(numlist, 3)
#[40, 20, 30]