I\'m creating a wxWidget application with C++ where at the start of the program I want the application window to contain pixels with random colors like this:
The issue here is that you constantly recreate and seed the random number generator in rand_node_colour
. You call it in a tight loop so you can get the same time which means the seed will be the same and that means the random numbers generated will be the same.
What you need to do is seed the generator once and then keep using its random output. An easy way to fix you code would be to make it static
in the function sos it is only initialized once and each subsequent call to the function will continue on instead of start the generator all over. If we do that the code becomes
void rand_node_colour(int nodeWeights[])
{
// construct a trivial random generator engine from a time-based seed:
static std::default_random_engine generator (std::chrono::system_clock::now().time_since_epoch().count());
std::uniform_int_distribution<int> distribution(0,255);
for(int i=0; i<3; i++)
{
nodeWeights[i] = distribution(generator);
}
}