Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | numeric‎ | random
m (Text replace - "{{tdcl list end" to "{{dcl list end")
m (MinGW -> MinGW-w64)
 
(34 intermediate revisions by 15 users not shown)
Line 1: Line 1:
 
{{cpp/title|random_device}}
 
{{cpp/title|random_device}}
{{cpp/numeric/random/random_device/sidebar}}
+
{{cpp/numeric/random/random_device/navbar}}
{{ddcl | header=random | notes={{mark since c++11}} | 1=
+
{{ddcl|header=random|since=c++11|1=
 
class random_device;
 
class random_device;
 
}}
 
}}
  
{{tt|std::random_device}} is a uniformly-distributed integer random number generator, which produces non-deterministic random numbers, if a non-deterministic source (e.g. a hardware device) is available to the implementation.
+
{{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 {{ttt|std::random_device}} object may generate the same number sequence.
  
 
===Member types===
 
===Member types===
{{dcl list begin}}
+
{{dsc begin}}
{{tdcl list hitem | Member type | Definition}}
+
{{dsc hitem|Member type|Definition}}
{{tdcl list item | {{tt|result_type}} | {{c|unsigned int}}}}
+
{{dsc|{{tt|result_type}} {{mark c++11}}|{{co|unsigned int}}}}
{{dcl list end}}
+
{{dsc end}}
  
 
===Member functions===
 
===Member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list h2 | Construction}}
+
{{dsc h2|Construction}}
{{dcl list template | cpp/numeric/random/engine/dcl list constructor |random_device}}
+
{{dsc inc|cpp/numeric/random/engine/dsc constructor|random_device}}
{{dcl list 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}}
  
{{dcl list h2 | Generation}}
+
{{dsc h2|Generation}}
{{dcl list template | cpp/numeric/random/engine/dcl list operator() | random_device}}
+
{{dsc inc|cpp/numeric/random/engine/dsc operator()|random_device}}
  
{{dcl list h2 | Characteristics}}
+
{{dsc h2|Characteristics}}
{{dcl list template | cpp/numeric/random/random_device/dcl list entropy}}
+
{{dsc inc|cpp/numeric/random/random_device/dsc entropy}}
{{dcl list template | cpp/numeric/random/engine/dcl list min |random_device}}
+
{{dsc inc|cpp/numeric/random/engine/dsc min|random_device}}
{{dcl list template | cpp/numeric/random/engine/dcl list max |random_device}}
+
{{dsc inc|cpp/numeric/random/engine/dsc max|random_device}}
{{dcl list 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=
+
|code=
 
#include <iostream>
 
#include <iostream>
#include <string>
 
 
#include <map>
 
#include <map>
 
#include <random>
 
#include <random>
 +
#include <string>
 +
 
int main()
 
int main()
 
{
 
{
 
     std::random_device rd;
 
     std::random_device rd;
 
     std::map<int, int> hist;
 
     std::map<int, int> hist;
     for(int n=0; n<20000; ++n)
+
    std::uniform_int_distribution<int> dist(0, 9);
         ++hist[rd()%10];
+
 
     for(auto p : hist)
+
     for (int n = 0; n != 20000; ++n)
         std::cout << p.first << " : " << std::string(p.second/100, '*') << '\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';
 
}
 
}
| output=
+
|p=true
 +
|output=
 
0 : ********************
 
0 : ********************
 
1 : *******************
 
1 : *******************
Line 56: Line 70:
 
9 : ********************
 
9 : ********************
 
}}
 
}}
 +
 +
{{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) [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 : ********************