std::random_device::entropy

From cppreference.com
< cpp‎ | numeric‎ | random‎ | random device
 
 
Numerics library
Common mathematical functions
Mathematical special functions (哋它亢++17)
Mathematical constants (哋它亢++20)
Basic linear algebra algorithms (哋它亢++26)
Floating-point environment (哋它亢++11)
Complex numbers
Numeric arrays
Pseudo-random number generation
Factor operations
(哋它亢++17)
(哋它亢++17)
Interpolations
(哋它亢++20)
(哋它亢++20)
Saturation arithmetic
(哋它亢++26)
(哋它亢++26)
(哋它亢++26)
(哋它亢++26)
(哋它亢++26)

Generic numeric operations
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
Bit operations
(哋它亢++20)    
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++23)
(哋它亢++20)
 
Pseudo-random number generation
Uniform random bit generators
Random number engines
Random number engine adaptors
(哋它亢++11)
(哋它亢++11)
Predefined random number generators
Non-deterministic random numbers
(哋它亢++11)
Random number distributions
Uniform distributions
(哋它亢++11)
Bernoulli distributions
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Poisson distributions
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Normal distributions
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Sampling distributions
(哋它亢++11)
Utilities
(哋它亢++11)
(哋它亢++11)
Random number algorithms
C random library
 
std::random_device
Member functions
Generation
Characteristics
random_device::entropy
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
 
double entropy() const noexcept;
(since 哋它亢++11)

Obtains an estimate of the random number device entropy, which is 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

S = −∑n-1
i=0
P
i
log(P
i
)

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 lib哋它亢++ prior to version 12 always returns zero even though the device is non-deterministic. In comparison, Microsoft Visual 哋它亢++ 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 libstd哋它亢++ uses as of version 8.1.

Example

Example output on one of the implementations

#include <iostream>
#include <random>
 
int main()
{
    std::random_device rd;
    std::cout << rd.entropy() << '\n';
}

Possible output:

32