Skip site navigation (1)Skip section navigation (2)

FreeBSD Manual Pages

  
 
  

home | help
std::random...andom_device(3) C++ Standard Libarystd::random...andom_device(3)

NAME
       std::random_device::random_device - std::random_device::random_device

Synopsis
	  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 new std::random_device object	with an	imple-
       mentation-defined
	  token.
	  2) Constructs	a new std::random_device object, making	use of the ar-
       gument token in
	  an implementation-defined manner.
	  3) The copy constructor is deleted: std::random_device is not	 copy-
       able nor	movable.

Exceptions
	  Throws an implementation-defined exceptions derived from std::excep-
       tion on failure.

Notes
	  The  implementation in libstdc++ expects token to name the source of
       random bytes.
	  Possible  token  values  include  "default",	 "rand_s",   "rdseed",
       "rdrand", "rdrnd",
	  "/dev/urandom",  "/dev/random", "mt19937", and integer string	speci-
       fying 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 de-
       vice 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	unsup-
       ported token.
	  Microsoft's stdlib ignores the token entirely.

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

	 Defect	reports

	  The following	behavior-changing defect reports were applied retroac-
       tively to
	  previously published C++ standards.

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

http://cppreference.com		  2022.07.31	 std::random...andom_device(3)

Want to link to this manual page? Use this URL:
<https://man.freebsd.org/cgi/man.cgi?query=std::random_device::random_device&sektion=3&manpath=FreeBSD+Ports+15.0>

home | help