-1

The random_device class is not recognized as part of the std:: namespace until I include <bits/stdc++.h>.

I want to ask you if there is another solution for this, because I don't really want to include all standard libraries for this purpose.

std::random_device rd;
std::mt19937 gen(rd()); 
std::uniform_int_distribution<> dis(1,5); 
std::cout<<dis(gen);
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
James Dean
  • 73
  • 8

1 Answers1

2

It is a good sign, indeed, that you don't want to include all the Standard Library header files (via the appalling and non-standard #include <bits/stdc++.h> concoction)! However, you do need to (explicitly) include the headers relevant to the aspects/components of the Standard Library that you actually use.

In the case of the std::random_device class (and, also, the other two random-related classes that you use), you will need to include the header that defines those classes, with the following line:

#include <random>

Also, for the std::cout (and related) classes, you will need #include <iostream>.

As mentioned in the comments, the cppreference website is as good a place as any to get a quick guide to which header(s) you need to include for any of the std::xxxx classes, although you will soon 'get to know' the relevant headers for the more common Standard Library classes.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83