Skip to content

Commit

Permalink
Don't use a fixed random seed by default
Browse files Browse the repository at this point in the history
This would lead to confusing results for non-advanced users, who would
expect to see different results per execution by default

Also use modern c++11 random classes
  • Loading branch information
nyalldawson committed Mar 31, 2020
1 parent 13f5bdd commit 51f22a2
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/analysis/processing/qgsalgorithmrandompointsonlines.cpp
Expand Up @@ -71,7 +71,7 @@ void QgsRandomPointsOnLinesAlgorithm::initAlgorithm( const QVariantMap & )
maxAttemptsParam->setFlags( maxAttemptsParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
addParameter( maxAttemptsParam.release() );

std::unique_ptr< QgsProcessingParameterNumber > randomSeedParam = qgis::make_unique< QgsProcessingParameterNumber >( SEED, QObject::tr( "Random seed" ), QgsProcessingParameterNumber::Integer, 1, false, 1 );
std::unique_ptr< QgsProcessingParameterNumber > randomSeedParam = qgis::make_unique< QgsProcessingParameterNumber >( SEED, QObject::tr( "Random seed" ), QgsProcessingParameterNumber::Integer, QVariant(), true, 1 );
randomSeedParam->setFlags( randomSeedParam->flags() | QgsProcessingParameterDefinition::FlagAdvanced );
addParameter( randomSeedParam.release() );

Expand Down Expand Up @@ -131,6 +131,7 @@ bool QgsRandomPointsOnLinesAlgorithm::prepareAlgorithm( const QVariantMap &param
mNumPoints = parameterAsInt( parameters, POINTS_NUMBER, context );
mMinDistance = parameterAsDouble( parameters, MIN_DISTANCE, context );
mMaxAttempts = parameterAsInt( parameters, MAX_TRIES_PER_POINT, context );
mUseRandomSeed = parameters.value( SEED ).isValid();
mRandSeed = parameterAsInt( parameters, SEED, context );
mIncludeLineAttr = parameterAsBoolean( parameters, INCLUDE_LINE_ATTRIBUTES, context );
return true;
Expand All @@ -155,7 +156,9 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap
throw QgsProcessingException( invalidSinkError( parameters, OUTPUT ) );

//initialize random engine
srand( mRandSeed );
std::random_device rd;
std::mt19937 mt( !mUseRandomSeed ? rd() : mRandSeed );
std::uniform_real_distribution<> uniformDist( 0, 1 );

//index for finding close points (mMinDistance > 0)
QgsSpatialIndex index;
Expand Down Expand Up @@ -217,7 +220,7 @@ QVariantMap QgsRandomPointsOnLinesAlgorithm::processAlgorithm( const QVariantMap
break;
}
// Generate a random point
double randPos = lineLength * ( double ) rand() / RAND_MAX;
double randPos = lineLength * uniformDist( mt );
QgsGeometry rpGeom = QgsGeometry( lGeom.interpolate( randPos ) );

if ( !rpGeom.isNull() && !rpGeom.isEmpty() )
Expand Down

0 comments on commit 51f22a2

Please sign in to comment.