Skip to content

Commit

Permalink
Replace use of rand with qrand
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Feb 10, 2015
1 parent 1c2c900 commit 8e4368c
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 34 deletions.
2 changes: 1 addition & 1 deletion src/app/main.cpp
Expand Up @@ -438,7 +438,7 @@ int main( int argc, char *argv[] )
#endif

// initialize random number seed
srand( time( NULL ) );
qsrand( time( NULL ) );

/////////////////////////////////////////////////////////////////
// Command line options 'behaviour' flag setup
Expand Down
4 changes: 2 additions & 2 deletions src/app/qgisapp.cpp
Expand Up @@ -6936,10 +6936,10 @@ void QgisApp::dizzy()
QRectF rect = mMapCanvas->sceneRect();
if ( rect.x() < -d || rect.x() > d || rect.y() < -d || rect.y() > d )
return; // do not affect panning
rect.moveTo(( rand() % ( 2 * d ) ) - d, ( rand() % ( 2 * d ) ) - d );
rect.moveTo(( qrand() % ( 2 * d ) ) - d, ( qrand() % ( 2 * d ) ) - d );
mMapCanvas->setSceneRect( rect );
QTransform matrix;
matrix.rotate(( rand() % ( 2 * r ) ) - r );
matrix.rotate(( qrand() % ( 2 * r ) ) - r );
mMapCanvas->setTransform( matrix );
}

Expand Down
12 changes: 6 additions & 6 deletions src/app/qgsdiagramproperties.cpp
Expand Up @@ -414,9 +414,9 @@ void QgsDiagramProperties::addAttribute( QTreeWidgetItem * item )
newItem->setFlags( newItem->flags() & ~Qt::ItemIsDropEnabled );

//set initial color for diagram category
int red = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
int green = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
int blue = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
int red = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
int green = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
int blue = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
QColor randomColor( red, green, blue );
newItem->setBackground( 1, QBrush( randomColor ) );
mDiagramAttributesTreeWidget->addTopLevelItem( newItem );
Expand Down Expand Up @@ -761,9 +761,9 @@ void QgsDiagramProperties::showAddAttributeExpressionDialog()
newItem->setFlags( newItem->flags() & ~Qt::ItemIsDropEnabled );

//set initial color for diagram category
int red = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
int green = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
int blue = 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) );
int red = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
int green = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
int blue = 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) );
QColor randomColor( red, green, blue );
newItem->setBackground( 1, QBrush( randomColor ) );
mDiagramAttributesTreeWidget->addTopLevelItem( newItem );
Expand Down
9 changes: 3 additions & 6 deletions src/app/qgstipfactory.cpp
Expand Up @@ -245,8 +245,7 @@ void QgsTipFactory::addGenericTip( QgsTip theTip )
}
QgsTip QgsTipFactory::getTip()
{
srand( QTime::currentTime().msec() );
int myRand = rand();
int myRand = qrand();
int myValue = static_cast<int>( myRand % mAllTips.count() ); //range [0,(count-1)]
QgsTip myTip = mAllTips.at( myValue );
return myTip;
Expand All @@ -258,16 +257,14 @@ QgsTip QgsTipFactory::getTip( int thePosition )
}
QgsTip QgsTipFactory::getGenericTip()
{
srand( QTime::currentTime().msec() );
int myRand = rand();
int myRand = qrand();
int myValue = static_cast<int>( myRand % mGenericTips.count() ); //range [0,(count-1)]
QgsTip myTip = mGenericTips.at( myValue );
return myTip;
}
QgsTip QgsTipFactory::getGuiTip()
{
srand( QTime::currentTime().msec() );
int myRand = rand();
int myRand = qrand();
int myValue = static_cast<int>( myRand % mGuiTips.count() ); //range [0,(count-1)]
QgsTip myTip = mGuiTips.at( myValue );
return myTip;
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgsexpression.cpp
Expand Up @@ -452,7 +452,7 @@ static QVariant fcnRndF( const QVariantList& values, const QgsFeature*, QgsExpre
return QVariant();

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

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

static QVariant fcnLinearScale( const QVariantList& values, const QgsFeature*, QgsExpression* parent )
Expand Down
2 changes: 0 additions & 2 deletions src/core/symbology-ng/qgsgraduatedsymbolrendererv2.cpp
Expand Up @@ -840,8 +840,6 @@ static QList<double> _calcJenksBreaks( QList<double> values, int classes,
// is larger. This will produce a more representative sample for very large
// layers, but could end up being computationally intensive...

qsrand( time( 0 ) );

sample.resize( qMax( maximumSize, values.size() / 10 ) );

QgsDebugMsg( QString( "natural breaks (jenks) sample size: %1" ).arg( sample.size() ) );
Expand Down
2 changes: 1 addition & 1 deletion src/core/symbology-ng/qgssymbolv2.cpp
Expand Up @@ -164,7 +164,7 @@ QgsSymbolV2* QgsSymbolV2::defaultSymbol( QGis::GeometryType geomType )
if ( defaultSymbol == "" ||
QgsProject::instance()->readBoolEntry( "DefaultStyles", "/RandomColors", true ) )
{
s->setColor( QColor::fromHsv( rand() % 360, 64 + rand() % 192, 128 + rand() % 128 ) );
s->setColor( QColor::fromHsv( qrand() % 360, 64 + qrand() % 192, 128 + qrand() % 128 ) );
}

return s;
Expand Down
19 changes: 9 additions & 10 deletions src/core/symbology-ng/qgsvectorcolorrampv2.cpp
Expand Up @@ -336,7 +336,7 @@ QList<QColor> QgsVectorRandomColorRampV2::randomColors( int count,
int safeValMin = qMin( valMin, valMax );

//start hue at random angle
double currentHueAngle = 360.0 * ( double )rand() / RAND_MAX;
double currentHueAngle = 360.0 * ( double )qrand() / RAND_MAX;

for ( int i = 0; i < count; i++ )
{
Expand All @@ -346,8 +346,8 @@ QList<QColor> QgsVectorRandomColorRampV2::randomColors( int count,
currentHueAngle += 137.50776;
//scale hue to between hueMax and hueMin
h = qBound( 0, qRound(( fmod( currentHueAngle, 360.0 ) / 360.0 ) * ( safeHueMax - safeHueMin ) + safeHueMin ), 359 );
s = qBound( 0, ( rand() % ( safeSatMax - safeSatMin + 1 ) ) + safeValMax, 255 );
v = qBound( 0, ( rand() % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 255 );
s = qBound( 0, ( qrand() % ( safeSatMax - safeSatMin + 1 ) ) + safeValMax, 255 );
v = qBound( 0, ( qrand() % ( safeValMax - safeValMin + 1 ) ) + safeValMin, 255 );
colors.append( QColor::fromHsv( h, s, v ) );
}
return colors;
Expand All @@ -363,7 +363,6 @@ void QgsVectorRandomColorRampV2::updateColors()
QgsRandomColorsV2::QgsRandomColorsV2()
: mTotalColorCount( 0 )
{
srand( QTime::currentTime().msec() );
}

QgsRandomColorsV2::~QgsRandomColorsV2()
Expand Down Expand Up @@ -396,9 +395,9 @@ QColor QgsRandomColorsV2::color( double value ) const
}

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

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

//random offsets
double hueOffset = ( 360.0 * rand() / ( RAND_MAX + 1.0 ) );
double hueOffset = ( 360.0 * qrand() / ( RAND_MAX + 1.0 ) );

//try to maximise difference between hues. this is not an ideal implementation, as constant steps
//through the hue wheel are not visually perceived as constant changes in hue
Expand All @@ -424,8 +423,8 @@ void QgsRandomColorsV2::setTotalColorCount( const int colorCount )
for ( int idx = 0; idx < colorCount; ++ idx )
{
int h = qRound( currentHue ) % 360;
int s = ( rand() % ( DEFAULT_RANDOM_SAT_MAX - DEFAULT_RANDOM_SAT_MIN + 1 ) ) + DEFAULT_RANDOM_SAT_MIN;
int v = ( rand() % ( DEFAULT_RANDOM_VAL_MAX - DEFAULT_RANDOM_VAL_MIN + 1 ) ) + DEFAULT_RANDOM_VAL_MIN;
int s = ( qrand() % ( DEFAULT_RANDOM_SAT_MAX - DEFAULT_RANDOM_SAT_MIN + 1 ) ) + DEFAULT_RANDOM_SAT_MIN;
int v = ( qrand() % ( DEFAULT_RANDOM_VAL_MAX - DEFAULT_RANDOM_VAL_MIN + 1 ) ) + DEFAULT_RANDOM_VAL_MIN;
mPrecalculatedColors << QColor::fromHsv( h, s, v );
currentHue += hueStep;
}
Expand Down
10 changes: 6 additions & 4 deletions src/gui/raster/qgsrasterhistogramwidget.cpp
Expand Up @@ -366,14 +366,16 @@ void QgsRasterHistogramWidget::refreshHistogram()
mHistoColors << Qt::black; // first element, not used
QVector<QColor> myColors;
myColors << Qt::red << Qt::green << Qt::blue << Qt::magenta << Qt::darkYellow << Qt::cyan;
srand( myBandCountInt * 100 ); // make sure colors are always the same for a given band count
qsrand( myBandCountInt * 100 ); // make sure colors are always the same for a given band count
while ( myColors.size() <= myBandCountInt )
{
myColors <<
QColor( 1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) ),
1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) ),
1 + ( int )( 255.0 * rand() / ( RAND_MAX + 1.0 ) ) );
QColor( 1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) ),
1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) ),
1 + ( int )( 255.0 * qrand() / ( RAND_MAX + 1.0 ) ) );
}
//randomise seed again
qsrand( time( NULL ) );

// assign colors to each band, depending on the current RGB/gray band selection
// grayscale
Expand Down

0 comments on commit 8e4368c

Please sign in to comment.