std::random in C++ 11

std::random in C++ 11

I seem to be unable to get programs using std::random to work.I have been using icpc version 13.1.3

The follwong example (taken off the web) fails to compile with the command:icpc rand_test.cpp -std=c++11

The errors reported are:
rand_test.cpp(8): error: namespace "std" has no member "uniform_real_distribution"
      std::uniform_real_distribution<> dis(1, 2);
           ^
rand_test.cpp(8): error: expected an expression
      std::uniform_real_distribution<> dis(1, 2);
                                     ^
rand_test.cpp(10): error: identifier "dis" is undefined
          std::cout << dis(gen) << ' ';

Here's rand_test.cpp :

#include <random>
#include <iostream>

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(1, 2);
    for (int n = 0; n < 10; ++n) {
        std::cout << dis(gen) << ' ';
    }
    std::cout << '\n';
}

Does anybody have any suggestions ? Thanks,

- Amartya

6 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

What gcc compatibility are you getting (icpc -v).  Intel's compiler depends on gcc's headers for standard library support, and your version of gcc may be lacking that class. 

I tested your testcase with icpc 14.0 with gcc 4.7.3 headers and it compiles and runs with no problems.

Hi Amartya,

                  As Casey correctly pointed out Intel compiler is in compatible with GCC/g++ on Linux* side and Microsoft* cl compiler on Windows* platform, So you may need to find the appropriate version of gcc/g++ where the std::random is supported, I tried it on icc 13.1.3 with gcc 4.8 and was able to successfully compile and run the application :-

shv@dpdknf01:~/quad$ icpc std_random.cpp -std=c++11

shv@dpdknf01:~/quad$ cat std_random.cpp
#include <random>
#include <iostream>

int main()
{
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_real_distribution<> dis(1, 2);
    for (int n = 0; n < 10; ++n) {
        std::cout << dis(gen) << ' ';
    }
    std::cout << '\n';
}

shv@dpdknf01:~/quad$ ./a.out
1.29628 1.00773 1.46501 1.1728 1.68578 1.03533 1.71997 1.59737 1.12835 1.01291  

Regards,

Sukruth H V

Thanks for your posts ! The version of gcc on this machine seems to be an old one (4.4.6) which is what must be the cause of this problem.

Is there a way of working around this issue without having to install a newer version of gcc ?

Probably not one that is easiler / less work than installing a newer version of gcc.  At a minimum you could grab the headers from a newer gcc along with libstdc++ and try to get icpc to use that over the system libstdc++, but at the point you have the headers and libraries from a newer gcc you might as well have just installed it properly.

Thanks for the information Casey !!

Login to leave a comment.