Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
9 changes: 8 additions & 1 deletion src/core/symbology/qgssymbol.cpp
Expand Up @@ -48,6 +48,7 @@


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


inline inline
QgsProperty rotateWholeSymbol( double additionalRotation, const QgsProperty &property ) QgsProperty rotateWholeSymbol( double additionalRotation, const QgsProperty &property )
Expand Down Expand Up @@ -307,7 +308,13 @@ QgsSymbol *QgsSymbol::defaultSymbol( QgsWkbTypes::GeometryType geomType )
if ( defaultSymbol.isEmpty() || if ( defaultSymbol.isEmpty() ||
QgsProject::instance()->readBoolEntry( QStringLiteral( "DefaultStyles" ), QStringLiteral( "/RandomColors" ), true ) ) 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; return s;
Expand Down

0 comments on commit 872871f

Please sign in to comment.