std::random...andom_device(3)
okay so bestie, you opened std::random...andom_device(3) β the C++ Standard Libary manual page for std::random_device::random_device (yes, βLibaryβ is literally how itβs spelled up top, weβre keeping the typo π
). This page is short but it has opinions about tokens, and weβre getting into all of them.
π NAME π
std::random_device::random_device β std::random_device::random_device
thatβs the whole NAME line, iconic repetition, no notes β¨
πΈ Synopsis πΈ
hereβs the overload lineup, C++11 energy and all π
random_device() : random_device( /*implementation-defined*/ ) {} (1)
(since C++11)
explicit random_device( const std::string& token ); (2)
(since C++11)
random_device( const random_device& ) = delete; (3)
(since C++11)
(1)β Default constructs a newstd::random_deviceobject with an implementation-defined token.(2)β Constructs a newstd::random_deviceobject, making use of the argumenttokenin an implementation-defined manner.(3)β The copy constructor is deleted:std::random_deviceis not copyable nor movable.
π Exceptions π
Throws an implementation-defined exception derived from std::exception on failure. so if itβs gonna fail, it fails with mystery implementation-defined flavor π
π«Ά Notes π«Ά
okay, token tea time β β and bestie, the standard is not giving us one universal meaning for token. itβs giving implementation-defined.
- The implementation in
libstdc++expectstokento name the source of random bytes. - Possible token values include
"default","hw","rand_s","rdseed","rdrand","rdrnd","/dev/urandom","/dev/random","mt19937", and an integer string specifying the seed of themt19937engine. - Token values other than
"default"are only valid for certain targets.
The implementation in libc++, 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 libstdc++ and libc++ throw an exception if provided an unsupported token. Microsoft's stdlib ignores the token entirely. token chaos, truly π
π¦ Example π¦
Demonstrates commonly available types of std::random_device on Linux. The original page has a little // Run this code marker before the block, so hereβs that whole vibe π
#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 libstdc++, ignored in msvc++, might throw on libc++ (as of Nov 2022)
demo(std::random_device {"/dev/random"});
// Request non-blocking /dev/urandom, ensures that RDRAND is not used
// Works on libstdc++ and libc++, ignored in msvc++ (as of Nov 2022)
demo(std::random_device {"/dev/urandom"});
// Request "hw", will use hardware-based random generation like rdrand
// Works on libstdc++, ignored in msvc++, throws on libc++ (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 C++ standards.
- DR:
P0935R0Β· Applied to:C++11Β· Behavior as published: default constructor wasexplicitΒ· Correct behavior: madeimplicit
mwah π this page came from http://cppreference.com, dated 2024.06.10, and the footer page name is std::random...andom_device(3) β stay random, bestie πβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms