Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/numeric/random/random device"

From cppreference.com
< cpp‎ | numeric‎ | random
(port workaround code for mingw libstdc++ from cpprefjp(CC-BY, https://cpprefjp.github.io/reference/random/random_device.html))
m (MinGW -> MinGW-w64)
 
(17 intermediate revisions by 10 users not shown)
Line 1: Line 1:
 
{{cpp/title|random_device}}
 
{{cpp/title|random_device}}
 
{{cpp/numeric/random/random_device/navbar}}
 
{{cpp/numeric/random/random_device/navbar}}
{{ddcl | header=random | since=c++11 | 1=
+
{{ddcl|header=random|since=c++11|1=
 
class random_device;
 
class random_device;
 
}}
 
}}
Line 7: Line 7:
 
{{tt|std::random_device}} is a uniformly-distributed integer random number generator that produces non-deterministic random numbers.
 
{{tt|std::random_device}} is a uniformly-distributed integer random number generator that produces non-deterministic random numbers.
  
{{tt|std::random_device}} may be implemented in terms of an implementation-defined pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation. In this case each {{tt|std::random_device}} object may generate the same number sequence.
+
{{tt|std::random_device}} may be implemented in terms of an implementation-defined pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation. In this case each {{ttt|std::random_device}} object may generate the same number sequence.
  
 
===Member types===
 
===Member types===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc hitem | Member type | Definition}}
+
{{dsc hitem|Member type|Definition}}
{{dsc | {{tt|result_type}} | {{c|unsigned int}}}}
+
{{dsc|{{tt|result_type}} {{mark c++11}}|{{co|unsigned int}}}}
 
{{dsc end}}
 
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc h2 | Construction}}
+
{{dsc h2|Construction}}
{{dsc inc | cpp/numeric/random/engine/dsc constructor |random_device}}
+
{{dsc inc|cpp/numeric/random/engine/dsc constructor|random_device}}
{{dsc mem fun | nolink=true | operator{{=}} | notes={{mark|deleted}} | the assignment operator is deleted}}
+
{{dsc mem fun|nolink=true|operator{{=}}|notes={{mark|deleted}} {{mark c++11}}|the assignment operator is deleted}}
  
{{dsc h2 | Generation}}
+
{{dsc h2|Generation}}
{{dsc inc | cpp/numeric/random/engine/dsc operator() | random_device}}
+
{{dsc inc|cpp/numeric/random/engine/dsc operator()|random_device}}
  
{{dsc h2 | Characteristics}}
+
{{dsc h2|Characteristics}}
{{dsc inc | cpp/numeric/random/random_device/dsc entropy}}
+
{{dsc inc|cpp/numeric/random/random_device/dsc entropy}}
{{dsc inc | cpp/numeric/random/engine/dsc min |random_device}}
+
{{dsc inc|cpp/numeric/random/engine/dsc min|random_device}}
{{dsc inc | cpp/numeric/random/engine/dsc max |random_device}}
+
{{dsc inc|cpp/numeric/random/engine/dsc max|random_device}}
 
{{dsc end}}
 
{{dsc end}}
 +
 +
===Notes===
 +
A notable implementation where {{ttt|std::random_device}} is deterministic in old versions of MinGW-w64 ([https://sourceforge.net/p/mingw-w64/bugs/338/ bug 338], fixed since GCC 9.2). The latest MinGW-w64 versions can be downloaded from [https://gcc-mcf.lhmouse.com/ GCC with the MCF thread model].
  
 
===Example===
 
===Example===
 
{{example
 
{{example
| p=true
+
|code=
| code=
+
 
#include <iostream>
 
#include <iostream>
#include <string>
 
 
#include <map>
 
#include <map>
 
#include <random>
 
#include <random>
 +
#include <string>
  
 
int main()
 
int main()
Line 44: Line 46:
 
     std::map<int, int> hist;
 
     std::map<int, int> hist;
 
     std::uniform_int_distribution<int> dist(0, 9);
 
     std::uniform_int_distribution<int> dist(0, 9);
     for (int n = 0; n < 20000; ++n) {
+
 
         ++hist[dist(rd)]; // note: demo only: the performance of many  
+
     for (int n = 0; n != 20000; ++n)
 +
         ++hist[dist(rd)]; // note: demo only: the performance of many
 
                           // implementations of random_device degrades sharply
 
                           // implementations of random_device degrades sharply
 
                           // once the entropy pool is exhausted. For practical use
 
                           // once the entropy pool is exhausted. For practical use
                           // random_device is generally only used to seed  
+
                           // random_device is generally only used to seed
 
                           // a PRNG such as mt19937
 
                           // a PRNG such as mt19937
    }
+
 
     for (auto p : hist) {
+
     for (auto [x, y] : hist)
         std::cout << p.first << " : " << std::string(p.second/100, '*') << '\n';
+
         std::cout << x << " : " << std::string(y / 100, '*') << '\n';
    }
+
 
}
 
}
| output=
+
|p=true
 +
|output=
 
0 : ********************
 
0 : ********************
 
1 : *******************
 
1 : *******************
Line 68: Line 71:
 
}}
 
}}
  
 
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
=== Note ===
+
 
+
==== MinGW GCC(libstdc++) ====
+
 
+
In MinGW GCC(libstdc++), {{tt|std::random_device}} is implemented by {{tt|std::mt19937}}
+
that is non-deterministic.
+
 
+
As a workaround, you can use CryptGenRandom (Win32API).
+
 
+
{{example
+
| p=true
+
| code=
+
//! @file random_device.hpp
+
#pragma once
+
 
+
#include <random>
+
 
+
#if defined(__MINGW32__) && defined(__GNUC__) && !defined(__clang__)
+
 
+
#include <system_error>
+
#include <limits>
+
#include <string>
+
#include <Windows.h>
+
#include <wincrypt.h>
+
 
+
namespace workaround_mingw_gcc {
+
class random_device {
+
private:
+
  class crypt_context {
+
  private:
+
    HCRYPTPROV prov_;
+
  public:
+
    crypt_context(DWORD prov_type, LPCTSTR container = nullptr, LPCTSTR provider = nullptr, DWORD flags = 0) {
+
      const auto success = ::CryptAcquireContext(&this->prov_, container, provider, prov_type, flags);
+
      if (!success) {
+
        throw std::system_error(
+
          std::error_code(::GetLastError(), std::system_category()),
+
          "CryptAcquireContext:(" + std::to_string(success) + ')'
+
        );
+
      }
+
    }
+
    crypt_context(const crypt_context&) = delete;
+
    void operator=(const crypt_context&) = delete;
+
    ~crypt_context() noexcept {
+
      ::CryptReleaseContext(this->prov_, 0);
+
    }
+
    //HCRYPTPROV& get() noexcept { return this->prov_; }
+
    const HCRYPTPROV& get() const noexcept { return this->prov_; }
+
  };
+
  crypt_context prov_;
+
 
+
public:
+
  using result_type = unsigned int;
+
  explicit random_device(const std::string& /*token*/ = "workaround_mingw_gcc ")
+
  : prov_(PROV_RSA_FULL)
+
  {}
+
  random_device(const random_device&) = delete;
+
  void operator=(const random_device&) = delete;
+
  //~random_device() = default;
+
  double entropy() const noexcept { return 0.0; }
+
  result_type operator()() {
+
    result_type re;
+
    const auto success = ::CryptGenRandom(this->prov_.get(), sizeof(re), reinterpret_cast<BYTE*>(&re));
+
    if (!success) {
+
      throw std::system_error(
+
        std::error_code(::GetLastError(), std::system_category()),
+
        "CryptGenRandom:(" + std::to_string(success) + ')'
+
      );
+
    }
+
    return re;
+
  }
+
  static constexpr result_type min() { return std::numeric_limits<result_type>::min(); }
+
  static constexpr result_type max() { return std::numeric_limits<result_type>::max(); }
+
};
+
} // namespace workaround_mingw_gcc
+
 
+
namespace cppreference {
+
using workaround_mingw_gcc::random_device;
+
}
+
 
+
#else //defined(__MINGW32__) && defined(__GNUC__) && !defined(__clang__)
+
 
+
namespace cppreference {
+
using std::random_device;
+
}
+
 
+
#endif //defined(__MINGW32__) && defined(__GNUC__) && !defined(__clang__)
+
}}
+
 
+
Usage:
+
 
+
{{example
+
| p=true
+
| code=
+
#include "random_device.hpp"
+
#include <iostream>
+
#include <array>
+
#include <functional>
+
#include <algorithm>
+
using seed_v_t = std::array<cppreference::random_device::result_type, sizeof(std::mt19937)/sizeof(cppreference::random_device::result_type)>;
+
seed_v_t create_seed_v()
+
{
+
  cppreference::random_device rnd;
+
  seed_v_t sed_v;
+
  std::generate(sed_v.begin(), sed_v.end(), std::ref(rnd));
+
  return sed_v;
+
}
+
std::mt19937 create_random_engine()
+
{
+
  const auto sed_v = create_seed_v();
+
  std::seed_seq seq(sed_v.begin(), sed_v.end());
+
  return std::mt19937(seq);
+
}
+
std::mt19937& random_engine() {
+
  static thread_local std::mt19937 engine = create_random_engine();
+
  return engine;
+
}
+
int main()
+
{
+
  std::mt19937& engine = random_engine();
+
  std::uniform_int_distribution<int> dist(1, 32);
+
  for(int i = 0; i < 10; ++i) std::cout << dist(engine) << std::endl;
+
}
+
}}
+
 
+
 
+
[[de:cpp/numeric/random/random device]]
+
[[es:cpp/numeric/random/random device]]
+
[[fr:cpp/numeric/random/random device]]
+
[[it:cpp/numeric/random/random device]]
+
[[ja:cpp/numeric/random/random device]]
+
[[pt:cpp/numeric/random/random device]]
+
[[ru:cpp/numeric/random/random device]]
+
[[zh:cpp/numeric/random/random device]]
+

Latest revision as of 21:34, 20 November 2023

 
 
 
 
 
Defined in header <random>
class random_device;
(since C++11)

std::random_device is a uniformly-distributed integer random number generator that produces non-deterministic random numbers.

std::random_device may be implemented in terms of an implementation-defined pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation. In this case each std::random_device object may generate the same number sequence.

Contents

[edit] Member types

Member type Definition
result_type (C++11) unsigned int

[edit] Member functions

Construction
constructs the engine
(public member function) [edit]
operator=
(deleted) (C++11)
the assignment operator is deleted
(public member function)
Generation
advances the engine's state and returns the generated value
(public member function) [edit]
Characteristics
(C++11)
obtains the entropy estimate for the non-deterministic random number generator
(public member function) [edit]
[static]
gets the smallest possible value in the output range
(public static member function) [edit]
[static]
gets the largest possible value in the output range
(public static member function) [edit]

[edit] Notes

A notable implementation where std::random_device is deterministic in old versions of MinGW-w64 (bug 338, fixed since GCC 9.2). The latest MinGW-w64 versions can be downloaded from GCC with the MCF thread model.

[edit] Example

#include <iostream>
#include <map>
#include <random>
#include <string>
 
int main()
{
    std::random_device rd;
    std::map<int, int> hist;
    std::uniform_int_distribution<int> dist(0, 9);
 
    for (int n = 0; n != 20000; ++n)
        ++hist[dist(rd)]; // note: demo only: the performance of many
                          // implementations of random_device degrades sharply
                          // once the entropy pool is exhausted. For practical use
                          // random_device is generally only used to seed
                          // a PRNG such as mt19937
 
    for (auto [x, y] : hist)
        std::cout << x << " : " << std::string(y / 100, '*') << '\n';
}

Possible output:

0 : ********************
1 : *******************
2 : ********************
3 : ********************
4 : ********************
5 : *******************
6 : ********************
7 : ********************
8 : *******************
9 : ********************