「Python」rand()で乱数生成のサンプル

環境
PyCharm 2021.3.3
Python 3.9.2

書式
np.random.rand() [0.0, 1.0)(0.0以上、1.0未満)の乱数を返します。
引数としてサイズを整数で返します。
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
import numpy as np
# 一つの乱数
rand = np.random.rand()
print("0.0~1.0未満の乱数生成")
print(rand)
# サイズが4の配列の乱数
res = np.random.rand(4)
print("サイズが4の配列の乱数生成")
print(res)
import numpy as np # 一つの乱数 rand = np.random.rand() print("0.0~1.0未満の乱数生成") print(rand) # サイズが4の配列の乱数 res = np.random.rand(4) print("サイズが4の配列の乱数生成") print(res)
import numpy as np
# 一つの乱数
rand = np.random.rand()
print("0.0~1.0未満の乱数生成")
print(rand)

# サイズが4の配列の乱数
res = np.random.rand(4)
print("サイズが4の配列の乱数生成")
print(res)

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
0.01.0未満の乱数生成
0.7379474114896076
サイズが4の配列の乱数生成
[0.25444331 0.21824842 0.57805348 0.25682291]
0.0~1.0未満の乱数生成 0.7379474114896076 サイズが4の配列の乱数生成 [0.25444331 0.21824842 0.57805348 0.25682291]
0.0~1.0未満の乱数生成
0.7379474114896076
サイズが4の配列の乱数生成
[0.25444331 0.21824842 0.57805348 0.25682291]

 

Python

Posted by arkgame