Difference between revisions of "cpp/numeric/random/random device"
From cppreference.com
(port workaround code for mingw libstdc++ from cpprefjp(CC-BY, https://cpprefjp.github.io/reference/random/random_device.html)) |
YexuanXiao (Talk | contribs) 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}} 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}} | + | {{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 | ||
− | + | |code= | |
− | + | ||
#include <iostream> | #include <iostream> | ||
− | |||
#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 | + | |
− | ++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 | + | for (auto [x, y] : hist) |
− | std::cout << | + | std::cout << x << " : " << std::string(y / 100, '*') << '\n'; |
− | + | ||
} | } | ||
− | + | |p=true | |
+ | |output= | ||
0 : ******************** | 0 : ******************** | ||
1 : ******************* | 1 : ******************* | ||
Line 68: | Line 71: | ||
}} | }} | ||
− | + | {{langlinks|de|es|fr|it|ja|pt|ru|zh}} | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + |
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) | |
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) | |
Characteristics | |
(C++11) |
obtains the entropy estimate for the non-deterministic random number generator (public member function) |
[static] |
gets the smallest possible value in the output range (public static member function) |
[static] |
gets the largest possible value in the output range (public static member function) |
[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
Run this code
#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 : ********************