[Python] 랜덀(Random) λͺ¨λ“ˆ 🎲

yunhΒ·2022λ…„ 5μ›” 14일
0

Python πŸ“–

λͺ©λ‘ 보기
34/34
post-thumbnail

🎲 랜덀(Random)

  • νŒŒμ΄μ¬μ—μ„œ λ‚œμˆ˜λ₯Ό λ°œμƒμ‹œν‚€κΈ° μœ„ν•΄ μ‚¬μš©.

  • 랜덀 λͺ¨λ“ˆμ„ import ν•œλ‹€.

    import random

랜덀 λ©”μ„œλ“œ

  • random.random()

    0.0 <= x < 1.0 μ‚¬μ΄μ˜ λžœλ€ν•œ μ‹€μˆ˜

  • random.uniform(a, b)

    a<= x <= b μ‚¬μ΄μ˜ λžœλ€ν•œ μ‹€μˆ˜

  • random.randint(a, b)

    a<= x< b μ‚¬μ΄μ˜ λžœλ€ν•œ μ •μˆ˜

  • random.randrange(a, b)

    a<= x< b μ‚¬μ΄μ˜ λžœλ€ν•œ μ •μˆ˜

  • random.randint(a, b)

    a<= x <= b μ‚¬μ΄μ˜ λžœλ€ν•œ μ •μˆ˜

  • random.choice(seq)

    seq 쀑 랜덀으둜 κ³ λ₯Έ ν•˜λ‚˜μ˜ μ›μ†Œ

  • random.sample(deq, n)

    seq 쀑 n개의 λžœλ€ν•œ μ›μ†Œλ“€μ˜ 배열을 리턴

  • random.shuffle(seq)

    seqκ°€ 랜덀으둜 μ„žμ–΄μ„œ 바꿔버린닀! λ°˜ν™˜ 값은 None

random λͺ¨λ“ˆ μ‚¬μš© μ˜ˆμ‹œ

import random

print(random.random())              # 0.8999159382861155
print(random.uniform(1, 5))         # 1.4874879446752813
print(random.randint(1, 5))         # 5
print(random.randrange(1, 5))       # 4

arr = ['a', 'b', 'c', 'd', 'e']
print(random.choice(arr))           # d
print(random.sample(arr, 2))        # ['b', 'd']
print(random.shuffle(arr))          # None
print(arr)                          # ['c', 'b', 'e', 'd', 'a']
profile
passionate developer

0개의 λŒ“κΈ€