When I create a random number (using 49 random number options) as per below - I always get the same result of 31 (but not when I use a number other than 49).
I have tried this on CodeRunner, CLion and CodeBlocks on my Mac and I get the error on all! When I try this on CodeBlocks on a PC then the number is random though...
Could this be an error with the compiler used by these apps on a Mac?
Exactly that - if I replace the 49 with another number then I get a different random number each time I run the program. But with 49 I always get 31 as the answer...
Strange hey??
I suspect you are indeed suffering the effects of this atrocious rand implementation
When I try this on CodeBlocks on a PC then the number is random though...
Yup. rand is often just awful. I don't even use it for toy implementations that don't matter anymore; people often learn code by reading existing code and the risk that someone would use then use rand for something important without knowing the risks is too high!
Yip, not using it even in toy applications due to others falling in the trap makes a lot of sense...
It is then still amazing that my university uses this... Guess they don't know any better. Or, maybe in a later module they will explain the better way and keep this in the first module just "because it is easy" (even if dodgy)...
I used to say, when confronted with it being used just for toy educational purposes, that it didn't really matter; it's obvious what it's meant to do, and using a bad implementation is fine because if people ever needed to actually generate high quality random numbers they wouldn't use rand() ; they'd do it properly.
However, having subsequently seen and read of serious instances in which shoddy random number generation was used through simply not knowing, I've come to the conclusion that people actually simply won't do it properly - generally because they just don't know. At least you now know.
When even the C standard says to not use it if there are better methods, rand is bad.
Quite a while ago someone gave me a link to a C++ working paper, "Three <random>-related Proposals"
http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2013/n3547.pdf
One of the suggestions was "add to <random> the following modest 4-part toolkit of novice-friendly functions," and they list the 4 functions.
This was proposed back in 2013, and as far as I know C++14 nor C++17 included the suggestion. I doubt even C++20 (or whatever it will be titled when approved) will include it. So I mashed up my own header version of the proposal:
#ifndef __RANDOM_TOOLKIT_HPP__
#define __RANDOM_TOOLKIT_HPP__
#include <random>
#include <chrono>
namespace rtk
{
std::default_random_engine& urng()
{
static std::default_random_engine URNG { };
return URNG;
}
void srand()
{
std::random_device rd;
unsigned seed;
if (rd.entropy() != 0) // the implementation provides a usable random device
{
seed = rd();
}
else // no random device available, seed using the system clock
{
seed = static_cast<unsigned> (std::chrono::system_clock::now().time_since_epoch().count());
}
urng().seed(seed);
}
int rand(int from, int to)
{
static std::uniform_int_distribution<> dist { };
using pt = decltype(dist)::param_type;
return dist(urng(), pt { from, to });
}
double rand(double from, double to)
{
static std::uniform_real_distribution<> dist { };
using pt = decltype(dist)::param_type;
return dist(urng(), pt { from, to });
}
}
#endif
I have done some testing with it and it seems to work. cheetoblue's example program could be reworked into (with a little extra to test if your compiler has implemented std::random_device [GCC variants don't]):
#include <iostream>
#include "random_toolkit.hpp"
int main()
{
// seed the random generator
rtk::srand();
// get a random integer number between 1 & 49 inclusive
int inum = rtk::rand(1, 49);
// get a random floating point number between 1 & 49 inclusive
double dnum = rtk::rand(1.0, 49.0);
std::cout << inum << ' ' << dnum << '\n';
// let's check std::random_device's entropy (0 means not implemented)
std::random_device rd;
if (0 != rd.entropy())
{
std::cout << "I'm alive!\n";
}
else
{
std::cout << "Doin' the dead cockroach!\n";
}
}
Off topic - but I would like to talk to cheetoblue. I am another Mac guy who is (slowly) learning to code in C++ to create some astronomy tools. You might look at this web page: