Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::generate_canonical

      From cppreference.com
      <cpp‎ |numeric‎ |random
       
       
       
      Pseudo-random number generation
       
      Defined in header<random>
      template<class RealType,std::size_t Bits,class Generator>
      RealType generate_canonical( Generator& g);
      (since C++11)

      Generates a random floating point number in range[01).

      To generate enough entropy,generate_canonical() will callg() exactly\(\small k\)k times, where\(\small k = \max(1, \lceil \frac{b}{\log_2 R} \rceil)\)k = max(1, ⌈ b / log2 R ⌉) and

      Contents

      [edit]Parameters

      g - generator to use to acquire entropy

      [edit]Return value

      Floating point value in range[01).

      [edit]Exceptions

      None except from those thrown byg.

      [edit]Notes

      Some existing implementations have a bug where they may occasionally return1.0 ifRealType isfloatGCC #63176LLVM #18767MSVC STL #1074. This isLWG issue 2524.

      [edit]Example

      Produce random numbers with 10 bits of randomness: this may produce onlyk* R distinct values.

      Run this code
      #include <iostream>#include <random> int main(){std::random_device rd;std::mt19937 gen(rd());for(int n=0; n<10;++n)std::cout<< std::generate_canonical<double,10>(gen)<<' ';std::cout<<'\n';}

      Possible output:

      0.208143 0.824147 0.0278604 0.343183 0.0173263 0.864057 0.647037 0.539467 0.0583497 0.609219

      [edit]See also

      produces real values evenly distributed across a range
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/numeric/random/generate_canonical&oldid=170228"

      [8]ページ先頭

      ©2009-2025 Movatter.jp