# std::random_device::entropy

> C++ Atlas: `cppatlas.com/w/cpp/numeric/random/random_device/entropy` | Source: https://en.cppreference.com/w/cpp/numeric/random/random_device/entropy | Domain: Numerics | Standards: C++11

Obtains an estimate of the random number device entropy, which is a floating-point value between 0 and log2(max()+1) (which is equal to std::numeric_limits<unsigned int>::digits).

## Official Specification Summary (cppreference)

```cpp
double entropy() const noexcept; // (since C++11)
```

Obtains an estimate of the random number device entropy, which is a floating-point value between 0 and log²(max()+1) (which is equal to `std::numeric_limits<unsigned int>::digits`). If the device has n states whose individual probabilities are P⁰,...,Pⁿ⁻¹, the device entropy S is defined as

S = −∑ⁿ⁻¹_(i=0) Pⁱlog(Pⁱ)

A deterministic random number generator (e.g. a pseudo-random engine) has entropy zero.

### Return value

The value of the device entropy, or zero if not applicable.

### 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`.

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.

### Example

Example output on one of the implementations

```cpp
#include <iostream>
#include <random>

int main()
{
    std::random_device rd;
    std::cout << rd.entropy() << '\n';
}
```

Possible output:

```text
32
```

---
Attribution: content from https://en.cppreference.com/w/cpp/numeric/random/random_device/entropy (cppreference.com), licensed under CC-BY-SA 3.0 and GFDL. Modified: converted to markdown, study notes added. Offline snapshot of 9 February 2025.