NumPy 的 random.uniform(~)
方法從均勻分布中采樣隨機值。換句話說,該方法返回一個隨機浮點數組。
注意
要生成從 0 到 1 的隨機浮點數,為了方便起見,請使用 random(~)
。
參數
1.low
| float
或類似數組的 float
| optional
下限(含)。默認情況下,low=0
。
2. high
| float
或類似數組的 float
上限(含)。默認情況下,high=1.0
。
3. size
| int
或 int
的元組 | optional
默認情況下,如果 low
和 high
均為浮點型,則返回單個值。
返回值
如果未指定 size
並且 low
和 high
都是標量,則返回單個 float
。否則,返回 NumPy 浮點數組。
例子
生成單個隨機浮點數
要生成 1(含)和 10(含)之間的單個隨機浮點數:
np.random.uniform(low=1, high=10)
8.841304420902002
生成多個隨機浮點數
要生成 1(含)和 10(含)之間的多個隨機浮點數:
np.random.uniform(low=1, high=10, size=2)
array([4.2625794 , 8.20604428])
相關用法
- Python unittest.mock.AsyncMock.assert_awaited_once_with用法及代碼示例
- Python unittest.TestCase.assertWarnsRegex用法及代碼示例
- Python unittest.mock.Mock.__class__用法及代碼示例
- Python Pandas unique方法用法及代碼示例
- Python unittest.TestCase.assertRaisesRegex用法及代碼示例
- Python unittest.mock.call用法及代碼示例
- Python unittest.mock.Mock.method_calls用法及代碼示例
- Python unittest.mock.Mock.call_args_list用法及代碼示例
- Python unittest.mock.AsyncMock.assert_any_await用法及代碼示例
- Python unittest.mock.Mock.assert_called用法及代碼示例
- Python unittest.TestCase.assertRaises用法及代碼示例
- Python unittest.TestCase.tearDownClass用法及代碼示例
- Python unittest.mock.Mock.assert_not_called用法及代碼示例
- Python unittest.IsolatedAsyncioTestCase用法及代碼示例
- Python NumPy unique方法用法及代碼示例
- Python unittest.TestCase.setUpClass用法及代碼示例
- Python unittest.mock.Mock.mock_calls用法及代碼示例
- Python unittest.mock.Mock.call_args用法及代碼示例
- Python unittest.mock.Mock.assert_has_calls用法及代碼示例
- Python unittest.mock.AsyncMock.assert_awaited_with用法及代碼示例
- Python unittest.mock.Mock.configure_mock用法及代碼示例
- Python unittest.mock.Mock.called用法及代碼示例
- Python unittest.mock.Mock.side_effect用法及代碼示例
- Python unittest.mock.Mock.assert_called_once_with用法及代碼示例
- Python unittest.mock.AsyncMock.assert_has_awaits用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 NumPy | uniform method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。