Skip to content

Commit 872871f

Browse files
committed
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.
1 parent 4646932 commit 872871f

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/core/symbology/qgssymbol.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848

4949
#include <cmath>
5050
#include <map>
51+
#include <random>
5152

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

313320
return s;

0 commit comments

Comments
 (0)