std::random...ice::entropy(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

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