Skip to content

Commit 8e4368c

Browse files
committed
Replace use of rand with qrand
1 parent 1c2c900 commit 8e4368c

9 files changed

+30
-34
lines changed

src/app/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ int main( int argc, char *argv[] )
438438
#endif
439439

440440
// initialize random number seed
441-
srand( time( NULL ) );
441+
qsrand( time( NULL ) );
442442

443443
/////////////////////////////////////////////////////////////////
444444
// Command line options 'behaviour' flag setup

src/app/qgisapp.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6936,10 +6936,10 @@ void QgisApp::dizzy()
69366936
QRectF rect = mMapCanvas->sceneRect();
69376937
if ( rect.x() < -d || rect.x() > d || rect.y() < -d || rect.y() > d )
69386938
return; // do not affect panning
6939-
rect.moveTo(( rand() % ( 2 * d ) ) - d, ( rand() % ( 2 * d ) ) - d );
6939+
rect.moveTo(( qrand() % ( 2 * d ) ) - d, ( qrand() % ( 2 * d ) ) - d );
69406940
mMapCanvas->setSceneRect( rect );
69416941
QTransform matrix;
6942-
matrix.rotate(( rand() % ( 2 * r ) ) - r );
6942+
matrix.rotate(( qrand() % ( 2 * r ) ) - r );
69436943
mMapCanvas->setTransform( matrix );
69446944
}
69456945

src/app/qgsdiagramproperties.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,9 @@ void QgsDiagramProperties::addAttribute( QTreeWidgetItem * item )
414414
newItem->setFlags( newItem->flags() & ~Qt::ItemIsDropEnabled );
415415

416416
//set initial color for diagram category
417-
int red = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
418-
int green = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
419-
int blue = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
417+
int red = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
418+
int green = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
419+
int blue = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
420420
QColor randomColor( red, green, blue );
421421
newItem->setBackground( 1, QBrush( randomColor ) );
422422
mDiagramAttributesTreeWidget->addTopLevelItem( newItem );
@@ -761,9 +761,9 @@ void QgsDiagramProperties::showAddAttributeExpressionDialog()
761761
newItem->setFlags( newItem->flags() & ~Qt::ItemIsDropEnabled );
762762

763763
//set initial color for diagram category
764-
int red = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
765-
int green = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
766-
int blue = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
764+
int red = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
765+
int green = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
766+
int blue = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
767767
QColor randomColor( red, green, blue );
768768
newItem->setBackground( 1, QBrush( randomColor ) );
769769
mDiagramAttributesTreeWidget->addTopLevelItem( newItem );

src/app/qgstipfactory.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,7 @@ void QgsTipFactory::addGenericTip( QgsTip theTip )
245245
}
246246
QgsTip QgsTipFactory::getTip()
247247
{
248-
srand( QTime::currentTime().msec() );
249-
int myRand = rand();
248+
int myRand = qrand();
250249
int myValue = static_cast<int>( myRand % mAllTips.count() ); //range [0,(count-1)]
251250
QgsTip myTip = mAllTips.at( myValue );
252251
return myTip;
@@ -258,16 +257,14 @@ QgsTip QgsTipFactory::getTip( int thePosition )
258257
}
259258
QgsTip QgsTipFactory::getGenericTip()
260259
{
261-
srand( QTime::currentTime().msec() );
262-
int myRand = rand();
260+
int myRand = qrand();
263261
int myValue = static_cast<int>( myRand % mGenericTips.count() ); //range [0,(count-1)]
264262
QgsTip myTip = mGenericTips.at( myValue );
265263
return myTip;
266264
}
267265
QgsTip QgsTipFactory::getGuiTip()
268266
{
269-
srand( QTime::currentTime().msec() );
270-
int myRand = rand();
267+
int myRand = qrand();
271268
int myValue = static_cast<int>( myRand % mGuiTips.count() ); //range [0,(count-1)]
272269
QgsTip myTip = mGuiTips.at( myValue );
273270
return myTip;

src/core/qgsexpression.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ static QVariant fcnRndF( const QVariantList& values, const QgsFeature*, QgsExpre
452452
return QVariant();
453453

454454
// Return a random double in the range [min, max] (inclusive)
455-
double f = ( double )rand() / RAND_MAX;
455+
double f = ( double )qrand() / RAND_MAX;
456456
return QVariant( min + f * ( max - min ) );
457457
}
458458
static QVariant fcnRnd( const QVariantList& values, const QgsFeature*, QgsExpression* parent )
@@ -463,7 +463,7 @@ static QVariant fcnRnd( const QVariantList& values, const QgsFeature*, QgsExpres
463463
return QVariant();
464464

465465
// Return a random integer in the range [min, max] (inclusive)
466-
return QVariant( min + ( rand() % ( int )( max - min + 1 ) ) );
466+
return QVariant( min + ( qrand() % ( int )( max - min + 1 ) ) );
467467
}
468468

469469
static QVariant fcnLinearScale( const QVariantList& values, const QgsFeature*, QgsExpression* parent )

src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -840,8 +840,6 @@ static QList<double> _calcJenksBreaks( QList<double> values, int classes,
840840
// is larger. This will produce a more representative sample for very large
841841
// layers, but could end up being computationally intensive...
842842

843-
qsrand( time( 0 ) );
844-
845843
sample.resize( qMax( maximumSize, values.size() / 10 ) );
846844

847845
QgsDebugMsg( QString( "natural breaks (jenks) sample size: %1" ).arg( sample.size() ) );

src/core/symbology-ng/qgssymbolv2.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ QgsSymbolV2* QgsSymbolV2::defaultSymbol( QGis::GeometryType geomType )
164164
if ( defaultSymbol == "" ||
165165
QgsProject::instance()->readBoolEntry( "DefaultStyles", "/RandomColors", true ) )
166166
{
167-
s->setColor( QColor::fromHsv( rand() % 360, 64 + rand() % 192, 128 + rand() % 128 ) );
167+
s->setColor( QColor::fromHsv( qrand() % 360, 64 + qrand() % 192, 128 + qrand() % 128 ) );
168168
}
169169

170170
return s;

src/core/symbology-ng/qgsvectorcolorrampv2.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ QList<QColor> QgsVectorRandomColorRampV2::randomColors( int count,
336336
int safeValMin = qMin( valMin, valMax );
337337

338338
//start hue at random angle
339-
double currentHueAngle = 360.0 * ( double )rand() / RAND_MAX;
339+
double currentHueAngle = 360.0 * ( double )qrand() / RAND_MAX;
340340

341341
for ( int i = 0; i < count; i++ )
342342
{
@@ -346,8 +346,8 @@ QList<QColor> QgsVectorRandomColorRampV2::randomColors( int count,
346346
currentHueAngle += 137.50776;
347347
//scale hue to between hueMax and hueMin
348348
h = qBound( 0, qRound(( fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( safeHueMax - safeHueMin ) + safeHueMin ), 359 );
349-
s = qBound( 0, ( rand() % ( safeSatMax - safeSatMin + 1 ) ) + safeValMax, 255 );
350-
v = qBound( 0, ( rand() % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 255 );
349+
s = qBound( 0, ( qrand() % ( safeSatMax - safeSatMin + 1 ) ) + safeValMax, 255 );
350+
v = qBound( 0, ( qrand() % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 255 );
351351
colors.append( QColor::fromHsv( h, s, v ) );
352352
}
353353
return colors;
@@ -363,7 +363,6 @@ void QgsVectorRandomColorRampV2::updateColors()
363363
QgsRandomColorsV2::QgsRandomColorsV2()
364364
: mTotalColorCount( 0 )
365365
{
366-
srand( QTime::currentTime().msec() );
367366
}
368367

369368
QgsRandomColorsV2::~QgsRandomColorsV2()
@@ -396,9 +395,9 @@ QColor QgsRandomColorsV2::color( double value ) const
396395
}
397396

398397
//can't use precalculated hues, use a totally random hue
399-
int h = 1 + ( int )( 360.0 * rand() / ( RAND_MAX + 1.0 ) );
400-
int s = ( rand() % ( DEFAULT_RANDOM_SAT_MAX - DEFAULT_RANDOM_SAT_MIN + 1 ) ) + DEFAULT_RANDOM_SAT_MIN;
401-
int v = ( rand() % ( maxVal - minVal + 1 ) ) + minVal;
398+
int h = 1 + ( int )( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
399+
int s = ( qrand() % ( DEFAULT_RANDOM_SAT_MAX - DEFAULT_RANDOM_SAT_MIN + 1 ) ) + DEFAULT_RANDOM_SAT_MIN;
400+
int v = ( qrand() % ( maxVal - minVal + 1 ) ) + minVal;
402401
return QColor::fromHsv( h, s, v );
403402
}
404403

@@ -412,7 +411,7 @@ void QgsRandomColorsV2::setTotalColorCount( const int colorCount )
412411
//similar colors being picked. TODO - investigate alternative "n-visually distinct color" routines
413412

414413
//random offsets
415-
double hueOffset = ( 360.0 * rand() / ( RAND_MAX + 1.0 ) );
414+
double hueOffset = ( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );
416415

417416
//try to maximise difference between hues. this is not an ideal implementation, as constant steps
418417
//through the hue wheel are not visually perceived as constant changes in hue
@@ -424,8 +423,8 @@ void QgsRandomColorsV2::setTotalColorCount( const int colorCount )
424423
for ( int idx = 0; idx < colorCount; ++ idx )
425424
{
426425
int h = qRound( currentHue ) % 360;
427-
int s = ( rand() % ( DEFAULT_RANDOM_SAT_MAX - DEFAULT_RANDOM_SAT_MIN + 1 ) ) + DEFAULT_RANDOM_SAT_MIN;
428-
int v = ( rand() % ( DEFAULT_RANDOM_VAL_MAX - DEFAULT_RANDOM_VAL_MIN + 1 ) ) + DEFAULT_RANDOM_VAL_MIN;
426+
int s = ( qrand() % ( DEFAULT_RANDOM_SAT_MAX - DEFAULT_RANDOM_SAT_MIN + 1 ) ) + DEFAULT_RANDOM_SAT_MIN;
427+
int v = ( qrand() % ( DEFAULT_RANDOM_VAL_MAX - DEFAULT_RANDOM_VAL_MIN + 1 ) ) + DEFAULT_RANDOM_VAL_MIN;
429428
mPrecalculatedColors << QColor::fromHsv( h, s, v );
430429
currentHue += hueStep;
431430
}

src/gui/raster/qgsrasterhistogramwidget.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -366,14 +366,16 @@ void QgsRasterHistogramWidget::refreshHistogram()
366366
mHistoColors << Qt::black; // first element, not used
367367
QVector<QColor> myColors;
368368
myColors << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::darkYellow << Qt::cyan;
369-
srand( myBandCountInt * 100 ); // make sure colors are always the same for a given band count
369+
qsrand( myBandCountInt * 100 ); // make sure colors are always the same for a given band count
370370
while ( myColors.size() <= myBandCountInt )
371371
{
372372
myColors <<
373-
QColor( 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) ),
374-
1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) ),
375-
1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) ) );
373+
QColor( 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) ),
374+
1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) ),
375+
1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) ) );
376376
}
377+
//randomise seed again
378+
qsrand( time( NULL ) );
377379

378380
// assign colors to each band, depending on the current RGB/gray band selection
379381
// grayscale

0 commit comments

Comments
 (0)