Intel® C++ Compiler
Community support and assistance for creating C++ code that runs on platforms based on Intel® processors.

std::random in C++ 11

Amartya_B_
Beginner
1,146 Views

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

0 Kudos
5 Replies
Casey
Beginner
1,146 Views

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.

0 Kudos
Sukruth_H_Intel
Employee
1,146 Views

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

0 Kudos
Amartya_B_
Beginner
1,146 Views

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 ?

0 Kudos
Casey
Beginner
1,146 Views

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.

0 Kudos
Amartya_B_
Beginner
1,146 Views

Thanks for the information Casey !!

0 Kudos
Reply