Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::random_device::entropy

      From cppreference.com
      <cpp‎ |numeric‎ |random‎ |random device
       
       
       
      Pseudo-random number generation
       
       
      double entropy()constnoexcept;
      (since C++11)

      Obtains an estimate of the random number device entropy, which is a floating-point value between0 andlog2(max()+1) (which is equal tostd::numeric_limits<unsignedint>::digits). If the device hasn states whose individual probabilities areP0,...,Pn-1, the device entropyS is defined as

      S = −∑n-1
      i=0
      Pilog(Pi)

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

      [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,LLVM libc++ prior to version 12 always returns zero even though the device is non-deterministic. In comparison, Microsoft Visual C++ implementation always returns32, andboost.random returns10.

      The entropy of the Linux kernel device/dev/urandom may be obtained usingioctl RNDGETENTCNT — that is whatstd::random_device::entropy() inGNU libstdc++ uses as of version 8.1.

      [edit]Example

      Example output on one of the implementations

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

      Possible output:

      32
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/random/random_device/entropy&oldid=149905"

      [8]ページ先頭

      ©2009-2025 Movatter.jp