std::random_device::random_device - cppreference.com

##### cppreference.com

##### Search

Create account

##### Namespaces

- Page
- Discussion

##### Variants

##### Views

- View
- Edit
- History

##### Actions

# std::random_device::random_device

From cppreference.com
< cpp‎ | numeric‎ | random‎ | random device

explicit random_device(const std::string& token = /*implementation-defined*/ ); | (1) | (since C++11)
random_device(const random_device& ) = delete; | (2) | (since C++11)

1) Constructs a new std::random_device object, making use of the argument `token`, if provided, in implementation-defined manner.
2) The copy constructor is deleted: `std::random_device` is not copyable.

### [edit] Exceptions

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

### [edit] Notes

The implementations in libc++ and libstdc++ expect `token` to be the name of a character device that produces random numbers when read from, with the default value "/dev/urandom", although where the CPU command RDRND is available, libstdc++ uses that as the default.

### [edit] Example

Demonstrates the two commonly available types of `std::random_device` on Linux

Run this code

    #include <iostream>
    #include <random>

    int main()
    {

        std::uniform_int_distribution<int> d(0, 10);

        std::random_device rd1; // uses RDRND or /dev/urandom
        for(int n = 0; n < 10; ++n)
            std::cout << d(rd1) << ' ';
        std::cout << '\n';

        std::random_device rd2("/dev/random"); // much slower on Linux
        for(int n = 0; n < 10; ++n)
            std::cout << d(rd2) << ' ';
        std::cout << '\n';
    }

Possible output:

    7 10 7 0 4 4 6 9 4 7
    2 4 10 6 3 2 0 6 3 7

Retrieved from "http://en.cppreference.com/mwiki/index.php?title=cpp/numeric/random/random_device/random_device&oldid=67759"