std::random...ice::entropy(3)
Manual name: C++ Standard Libary β page std::random...ice::entropy(3). We're deep in C++ Standard Library land, and today's main character is std::random_device::entropy. It's giving randomness-estimate energy. π
β¨
πΈ NAME πΈ
std::random_device::entropy β std::random_device::entropy
Yes bestie, the man page literally says the name twice. We are honoring that. π«Ά
β¨ Synopsis β¨
double entropy() const noexcept; (since C++11)
Okay so this function obtains an estimate of the random number device entropy. It's a floating-point value between 0 and log 2(max()+1) (which is equal to std::numeric_limits<unsigned int>::digits). If the device has n states whose individual probabilities are P 0,...,P n-1, the device entropy S is defined as β and the page lays it out like this, line breaks and all:
S = ββn-1
i=0 P
ilog(P
i)
A deterministic random number generator (e.g. a pseudo-random engine) has entropy zero. Periodt. π
π Return value π
The value of the device entropy, or zero if not applicable. That's the whole return value, no cap. π«Ά
π§ Notes π§
This function is not fully implemented in some standard libraries. For example, LLVM libc++ prior to version 12 always returns zero even though the device is non-deterministic. In comparison, Microsoft Visual C++ implementation always returns 32, and boost.random returns 10. Not them all doing their own thing π
The entropy of the Linux kernel device /dev/urandom may be obtained using ioctl RNDGETENTCNT β that is what std::random_device::entropy() in GNU libstdc++ uses as of version 8.1. iykyk π
π Example π
Example output on one of the implementations. The page has a little // Run this code marker, then this:
#include <iostream>
#include <random>
int main()
{
std::random_device rd;
std::cout << rd.entropy() << '\n';
}
Possible output:
32
Slay, it printed a number. π
That's the whole page, bestie. Source/version/date sign-off: http://cppreference.com, 2024.06.10, and the title std::random...ice::entropy(3). πβ¨π
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms