2024-여름

[python] random 모듈

돌멩이수프 2024. 6. 23. 11:50
728x90

 

파이썬에서 랜덤을 표현하기 위한 함수에는 뭐가 있을까?

 

randint

주어진 최소/최대 범위 안에서 임의의 난수를 추출하려면 randint를 사용한다.

import numpy as np

a = np.random.randint(0,5)
a
2

 

 

rand

균등 분포에서 표본을 추출한다.

import numpy as np

arr = np.random.rand(5,5)
arr
array([[2.34051083e-01, 5.57773443e-01, 8.46181813e-01, 6.64954959e-01,
        6.33586601e-01],
       [6.78664190e-02, 3.94113444e-01, 4.02703066e-03, 7.17660102e-01,
        4.98200373e-01],
       [8.33651440e-02, 2.28083318e-01, 4.50839735e-01, 3.97756944e-01,
        5.54174748e-02],
       [4.71974019e-01, 4.62151922e-01, 8.69770556e-01, 4.63079762e-02,
        6.19319001e-04],
       [3.11196066e-01, 9.72571882e-01, 6.03108159e-01, 3.66606980e-03,
        4.65912288e-01]])

 

 

randn

 표준편차가 1이고 평균값이 0인 정규분포에서 표본을 추출한다.

import numpy as np

arr = np.random.randn(5,5)
arr
array([[-1.63957933, -2.14012784, -2.27664071, -1.17100271,  0.69130755],
       [-1.01028643, -0.95240592, -1.22742726, -1.19236635,  1.40487629],
       [-0.59497869, -1.33065626, -0.7716312 ,  1.37631266, -0.58325069],
       [-0.47514488,  0.33662749, -0.99822037, -0.66193678,  0.37183201],
       [-2.52861349,  1.6100306 , -0.44262319,  2.19748035, -0.43968747]])

 

 

 

 

728x90