<random> not working?

I've been trying to use the <random> header, as shown in the reference page here:

This is the code in question:

void DisplaySix(unsigned char iNumber)
  unsigned int iDisplay;
  default_random_engine engine;
    cout << iDisplay;
    cout << "\n";

I assume that the code above would produce and display a "random" number between 1 and 6 equal to X times, where X is the variable "iNumber" passed to the function.

I am using Code::Blocks (not sure if that matters).
When compiling I get these errors for each occurrence of the code above:

In function 'void DisplaySix(unsigned char)':
error: 'default_random_engine' was not declared in this scope
error: expected ';' before 'engine'
error: 'uniform_int_distribution' was not declared in this scope
error: expected primary-expression before 'int'
error: expected ';' before 'int'
error: 'engine' was not declared in this scope
error: 'distribution' was not declared in this scope

I tried removing the "using namespace std" line (thinking that in some way, it might help), and manually adding "std::" to each object in the std namespace, along with the "default_random_engine" and "uniform_int_distribution" objects as shown in the <random> reference page, which resulted in these errors as well:

error: 'default_random_engine' is not a member of 'std'
error: 'uniform_int_distribution' is not a member of 'std'

I'm wondering...is it something I've done wrong? How do you get these to work, or would it be better to just use rand() and modulus?
Note: I am trying to avoid using rand() and modulus due to the higher probability of lower results for more of a seemingly "random" number each time.

Thank you for any help and/or insight into what might be going wrong here.

Also, In the code example above, I have shown the "DisplaySix()" function of my program. However, in the entire source code, there are multiple functions, all using different max values. However, all of these functions have the same errors when compiling. So, if there is an easier/better way to get "random" numbers between 1 and X, please let me know.
I am using Code::Blocks (not sure if that matters).

It does matter. The code snippet works, here for example: http://ideone.com/VYuULv but only if the compiler supports C++11. If you don't have such compiler, you can use TR1 or boost.random, but most likely all you need to do is add -std=c++11 or -std=c++0x to your project options.
Last edited on
I apologize, I should have mentioned that in my original post.

It did tell me something about that file being experimental and needing to enable one of those options. So, I went to Settings -> Compiler and debugger... -> Compiler settings -> Compiler flags, and enabled the "Have g++ follow the coming C++0x ISO C++ language standard [-std=c++0x]" option.

I had that enabled when I tried to compile and got those errors. I checked again, and do not see an option for "-std=c++11".

Edit* I checked the link that you posted, and it does seem to work. So, maybe, the problem is in another part of the source code? I rewrote the program using rand() so that it would compile. I'm still interested in knowing what I did wrong though, so I'll write it back to the way it originally was, and post the entire code here.
Last edited on
Topic archived. No new replies allowed.