std::random_device:: entropy
| Common mathematical functions | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical special functions (C++17) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Mathematical constants (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Basic linear algebra algorithms (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Data-parallel types (SIMD) (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Floating-point environment (C++11) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Complex numbers | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Numeric array (
valarray
)
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Pseudo-random number generation | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Bit manipulation (C++20) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Saturation arithmetic (C++26) | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Factor operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Interpolations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Generic numeric operations | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| C-style checked integer arithmetic | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Member functions | ||||
| Generation | ||||
| Characteristics | ||||
|
random_device::entropy
|
||||
|
double
entropy
(
)
const
noexcept
;
|
(C++11 起) | |
获取随机数设备熵的估计值,该值是一个介于 0 和 log 2 (max()+1) 之间的浮点数值(等于 std:: numeric_limits < unsigned int > :: digits )。若设备具有 n 个状态,其各自概率为 P 0 ,...,P n-1 ,则设备熵 S 定义为
S = −∑
n-1
i=0
P
i
log(P
i
)
确定性随机数生成器(例如伪随机引擎)的熵为零。
返回值
设备熵值,若不适用则为零。
注释
此函数在某些标准库中并未完全实现。例如, LLVM libc++ 在12版本之前即使设备具有非确定性也始终返回零。相比之下,Microsoft Visual C++ 实现始终返回 32 ,而 boost.random 返回 10 。
Linux内核设备
/dev/urandom
的熵值可通过
ioctl RNDGETENTCNT
获取——这正是
GNU libstdc++
自8.1版本起
std::random_device::entropy()
方法的实现方式。
示例
示例输出(基于某个实现)
#include <iostream> #include <random> int main() { std::random_device rd; std::cout << rd.entropy() << '\n'; }
可能的输出:
32