std::random_device::entropy

From cppreference.com
 
 
 
 
std::random_device
Member functions
random_device::random_device
Generation
random_device::operator()
Characteristics
random_device::entropy
random_device::min
random_device::max
 
double entropy() const;
(since C++11)

Obtains an estimate of the random number device entropy, which is a floating-point value between min() and log
2
(max()+1)
. If the device has n states whose individual probabilities are P
0
,...,P
n-1
, the device probability 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.

Contents

[edit] Exceptions

noexcept specification:  
noexcept
  (since C++11)

[edit] Return value

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

[edit] Notes

This function is not fully implemented in some standard libraries. For example, gcc and clang always return zero even though the device is non-deterministic. In comparison, Visual Studio always returns 32, and boost.random returns 10.

[edit] 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