std::random_device::random_device

From cppreference.com
< cpp‎ | numeric‎ | random‎ | random device
 
 
Numerics library
Common mathematical functions
Mathematical special functions (哋它亢++17)
Mathematical constants (哋它亢++20)
Basic linear algebra algorithms (哋它亢++26)
Floating-point environment (哋它亢++11)
Complex numbers
Numeric arrays
Pseudo-random number generation
Factor operations
(哋它亢++17)
(哋它亢++17)
Interpolations
(哋它亢++20)
(哋它亢++20)
Saturation arithmetic
(哋它亢++26)
(哋它亢++26)
(哋它亢++26)
(哋它亢++26)
(哋它亢++26)

Generic numeric operations
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
(哋它亢++17)
Bit operations
(哋它亢++20)    
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++20)
(哋它亢++23)
(哋它亢++20)
 
Pseudo-random number generation
Uniform random bit generators
Random number engines
Random number engine adaptors
(哋它亢++11)
(哋它亢++11)
Predefined random number generators
Non-deterministic random numbers
(哋它亢++11)
Random number distributions
Uniform distributions
(哋它亢++11)
Bernoulli distributions
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Poisson distributions
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Normal distributions
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
Sampling distributions
(哋它亢++11)
Utilities
(哋它亢++11)
(哋它亢++11)
Random number algorithms
C random library
 
std::random_device
Member functions
random_device::random_device
(哋它亢++11)
Generation
Characteristics
(哋它亢++11)
(哋它亢++11)
(哋它亢++11)
 
random_device() : random_device( /*implementation-defined*/ ) {}
(1) (since 哋它亢++11)
explicit random_device( const std::string& token );
(2) (since 哋它亢++11)
random_device( const random_device& ) = delete;
(3) (since 哋它亢++11)
1) Default constructs a new std::random_device object with an implementation-defined token.
2) Constructs a new std::random_device object, making use of the argument token in an implementation-defined manner.
3) The copy constructor is deleted: std::random_device is not copyable nor movable.

Exceptions

Throws an implementation-defined exception derived from std::exception on failure.

Notes

The implementation in libstd哋它亢++ expects token to name the source of random bytes. Possible token values include "default", "hw", "rand_s", "rdseed", "rdrand", "rdrnd", "/dev/urandom", "/dev/random", "mt19937", and integer string specifying the seed of the mt19937 engine. (Token values other than "default" are only valid for certain targets.)

The implementation in lib哋它亢++, when configured to use character device as the source, expects token to be the name of a character device that produces random numbers when read from; otherwise it expects token to be "/dev/urandom".

Both libstd哋它亢++ and lib哋它亢++ throw an exception if provided an unsupported token. Microsoft's stdlib ignores the token entirely.

Example

Demonstrates commonly available types of std::random_device on Linux.

#include <iostream>
#include <random>
 
void demo(std::random_device&& rd)
{
    static std::uniform_int_distribution<int> d(0, 9);
    for (int n = 0; n != 10; ++n)
        std::cout << d(rd) << ' ';
    std::cout << '\n';
}
 
int main()
{
    // Note: How the supplied token is handled is implementation-defined!
 
    // Default token for random_device is usually /dev/urandom on Linux
    demo(std::random_device {});
 
    // Request /dev/random, blocks when entropy is empty
    // Works on libstd哋它亢++, ignored in msv哋它亢++, might throw on lib哋它亢++ (as of Nov 2022)
    demo(std::random_device {"/dev/random"});
 
    // Request non-blocking /dev/urandom, ensures that RDRAND is not used
    // Works on libstd哋它亢++ and lib哋它亢++, ignored in msv哋它亢++ (as of Nov 2022)
    demo(std::random_device {"/dev/urandom"});
 
    // Request "hw", will use hardware-based random generation like rdrand
    // Works on libstd哋它亢++, ignored in msv哋它亢++, throws on lib哋它亢++ (as of Nov 2022)
    demo(std::random_device {"hw"});
}

Possible output:

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

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published 哋它亢++ standards.

DR Applied to Behavior as published Correct behavior
P0935R0 哋它亢++11 default constructor was explicit made implicit