I've been trying to use the <random> header, as shown in the reference page here:
http://www.cplusplus.com/reference/random/
This is the code in question:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
void DisplaySix(unsigned char iNumber)
{
unsigned int iDisplay;
default_random_engine engine;
uniform_int_distribution<int>distribution(1,6);
do
{
iDisplay=distribution(engine);
cout << iDisplay;
cout << "\n";
iNumber--;
}
while(iNumber>0);
return;
}
|
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.