2

I've confirmed from here and (more definitively) here that you need to #include<random> to use std::mt19937, but when I do this in CLion, CLion flags it red and suggests that it's in <bits/random.h>. So which one is it? Am I supposed to #include both? But I thought header files with .h extension are deprecated?

By the way I'm using CLion 1.2.1 with GCC 4.8.3 on CentOS Linux release 7.1.1503.

Community
  • 1
  • 1
Ray
  • 7,833
  • 13
  • 57
  • 91

2 Answers2

3

It's <random>.

<bits/random.h> is GCC-specific implementation. Include only <random>.

CLion doing it universally by searching where missed class defined actually, for GCC it is <bits/random.h>, that's why it recommends to include it. It is not formally an error, but this is non-crossplatform and even non-crosscompiler.

vladon
  • 8,158
  • 2
  • 47
  • 91
  • OK. But the fact that CLion is flagging it red is still annoying. I'll see if upgrading GCC helps. – Ray Nov 19 '15 at 17:38
  • 1
    @Ray Just include `` header, wait some time till CLion rebuilds symbols, and it will not flag it. CLion is written in Java and is very slow :-( – vladon Nov 19 '15 at 17:39
2

You should upgrade your GCC (current version in november 2015 is GCC 5.2 and it has much better C++11 support) and have it invoked as g++ -std=c++11 (use also -Wall -Wextra -g to get all warnings, more of them, and debugging information) then of course use #include <random>, as documented here.

(upgrading to GCC 5.2 can be done by compiling it from its downloaded source code; ask some other question if you need to)

If you simply #include <bits/random.h> directly it is implementation specific (never include yourself a bits/*.h internal header) and IIRC you'll get an error (or at least a serious warning).

You don't need to use Clion. The compiler is GCC.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • GCC 4.8 should have . – interjay Nov 19 '15 at 17:36
  • Probably, but GCC 4.8 support for C++11 is incomplete. – Basile Starynkevitch Nov 19 '15 at 17:36
  • @BasileStarynkevitch But GCC 5.2 support for C++11 is incomplete too :-) – vladon Nov 19 '15 at 17:38
  • Possibly, but that doesn't seem relevant to this question (which seems to be a problem with CLion). – interjay Nov 19 '15 at 17:38
  • @vladon: indeed, but GCC 5.2 supports much better C++11 than GCC 4.8 – Basile Starynkevitch Nov 19 '15 at 17:39
  • @BasileStarynkevitch Indeed, but it's not GCC issue at all :-) GCC 4.8 correctly supports `` of C++11. – vladon Nov 19 '15 at 17:40
  • Hmm it does seem to me to be a GCC problem, I've tried official example code from cppreference and it doesn't compile. http://en.cppreference.com/w/cpp/numeric/random/uniform_real_distribution – Ray Nov 19 '15 at 18:10
  • @Ray: you should upgrade your GCC. Don't forget the `-std=c++11` option, it is essential (default is C++1998 in GCC 4.8). And show the exact compilation command line you are using in your question. – Basile Starynkevitch Nov 19 '15 at 18:17
  • @BasileStarynkevitch as far as I can see, all the ways of upgrading GCC to 5.2 are extremely long, convoluted and cryptic, so I'll just have to live with archaic methods of generating random numbers for now. If you know of an easy way to upgrade to GCC 5.2 then please please let me know. – Ray Nov 20 '15 at 16:43
  • Yes, install the source dependencies for your GCC 4.8. Then get the source code of GCC 5.2 and build it (in a different build tree). If you are unfamiliar with the process of building free software from source code, ask another question (perhaps elsewhere) – Basile Starynkevitch Nov 20 '15 at 17:49
  • @Ray It will compile ok with `-std=c++11` switch. – vladon Nov 22 '15 at 08:59