👻

# 【Python】【初心者】numpyで乱数を発生させるメソッドの整理

2022/11/13に公開

numpyで乱数を発生させるメソッド

ざっくりまとめると、以下の通りです。

ひとつひとつ実際の動きを見ていきましょう

# rand()

``````np.random.rand()
``````

``````0.11687868813774382
``````

``````np.random.rand(5)
``````

``````array([0.42817233, 0.06407754, 0.59819277, 0.98643563, 0.35469029])
``````

``````np.random.rand(5,2)
``````

``````array([[0.80340624, 0.19355833],
[0.69710716, 0.96601196],
[0.22048461, 0.37884308],
[0.22375278, 0.13003753],
[0.99764547, 0.81994001]])
``````

# random_sample()

rand()と同じです

``````np.random.random_sample()
``````

``````0.4483261487002377
``````

rand(x)と同じです

``````np.random.random_sample(5)
``````

``````array([0.42973122, 0.92241316, 0.01998666, 0.52651084, 0.93452441])
``````

rand()と違って,タプルで指定する必要があります

``````np.random.random_sample((5,2))
``````

``````array([[0.48341019, 0.44231109],
[0.67387778, 0.29545236],
[0.96660802, 0.47673148],
[0.90473489, 0.65761039],
[0.90910482, 0.22503467]])
``````

# randint()

``````np.random.randint(100)
``````

``````39
``````

``````np.random.randint(50,100,5)
``````

``````array([65, 98, 85, 68, 58])
``````

``````np.random.randint(50,100,(5,2))
``````

``````array([[52, 74],
[55, 99],
[59, 94],
[63, 72],
[52, 57]])
``````

# randn()

``````np.random.randn()
``````

``````-0.4339820081684717
``````

``````np.random.randn(5)
``````

``````array([ 1.32869547, -1.00305495, -1.30708037,  0.12537492, -0.00227023])
``````

``````np.random.randn(5,2)
``````

``````rray([[-0.8395241 ,  1.02262407],
[-1.82382528, -0.90688112],
[ 0.91264836,  0.60627436],
[-0.77165503, -1.61145402],
[ 0.39865203,  1.94673226]])
``````

ログインするとコメントできます