How to generate random numbers using "random_device"?

HI!
What is wrong with this piece of code :

1
2
3
4
5
6
7
8
9
10
#include <random>
#include <iostream>

using namespace std ;

int main ()
{
    random_device myrd ;
    cout << "\nRandom : " << myrd () ;
}


I copied and pasted the same thing from this site reference to CodeBlocks, but there are lots of errors ;

There is a strange error :
error This file requires compiler and library support for the \
ISO C++ 2011 standard. This support is currently experimental, and must be \
enabled with the -std=c++11 or -std=gnu++11 compiler options.


So I think my COdeBlocks is missing something, right?

There are some other errors :

line 8 - random_device was not declared in this scope
line 8 - expected ';' before 'myrd'
line 9 - 'myrd' was not declared in this scope


There is definitely something wrong with my compiler. Because I defined them all ;
You should add -std=c++11 to your compiler arguments. That's exactly what your error says, this function is only available in the c++11 standard.
And if you don't need perfectly unique numbers just use rand()
Last edited on
How should I add "-std=c++11" to my project?
Should I download it?
Or ...

(Sorry for the off-topic questions)
In Code::Blocks you go to settings -> compiler -> and you check the box with apply the c++11 standard
Topic archived. No new replies allowed.