Skip to content
Permalink
Browse files
Make sure we use get uniquely seeded random numbers to generate
random symbol colors

Since qsrand is thread-specific, we can't rely on qrand() to return
uniquely seeded random numbers when not in the main thread.

Instead, just use the c++11 random number generators to get better
quality random colors for new layers.

Fixes processing outputs always have the same (yuck) brown color.
  • Loading branch information
nyalldawson committed Feb 3, 2018
1 parent 4646932 commit 872871f
Showing 1 changed file with 8 additions and 1 deletion.
@@ -48,6 +48,7 @@

#include <cmath>
#include <map>
#include <random>

inline
QgsProperty rotateWholeSymbol( double additionalRotation, const QgsProperty &property )
@@ -307,7 +308,13 @@ QgsSymbol *QgsSymbol::defaultSymbol( QgsWkbTypes::GeometryType geomType )
if ( defaultSymbol.isEmpty() ||
QgsProject::instance()->readBoolEntry( QStringLiteral( "DefaultStyles" ), QStringLiteral( "/RandomColors" ), true ) )
{
s->setColor( QColor::fromHsv( qrand() % 360, 64 + qrand() % 192, 128 + qrand() % 128 ) );
// Make sure we use get uniquely seeded random numbers, and not the same sequence of numbers
std::random_device rd;
std::mt19937 mt( rd() );
std::uniform_int_distribution<int> hueDist( 0, 359 );
std::uniform_int_distribution<int> satDist( 64, 255 );
std::uniform_int_distribution<int> valueDist( 128, 255 );
s->setColor( QColor::fromHsv( hueDist( mt ), satDist( mt ), valueDist( mt ) ) );
}

return s;

0 comments on commit 872871f

Please sign in to comment.