std::random...andom_device(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

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. (1) β€” Default constructs a new std::random_device object with an implementation-defined token.
  2. (2) β€” Constructs a new std::random_device object, making use of the argument token in an implementation-defined manner.
  3. (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. 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++ 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 an integer string specifying the seed of the mt19937 engine.
  • 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 was explicit Β· Correct behavior: made implicit

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