Skip to content

Commit

Permalink
followup: implementation using stdlib
Browse files Browse the repository at this point in the history
  • Loading branch information
olivierdalang committed Dec 3, 2019
1 parent cfe729e commit 800198b
Showing 1 changed file with 20 additions and 20 deletions.
40 changes: 20 additions & 20 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -13,6 +13,9 @@
* *
***************************************************************************/


#include <random>

#include "qgscoordinateformatter.h"
#include "qgsexpressionfunction.h"
#include "qgsexpressionutils.h"
Expand Down Expand Up @@ -366,18 +369,16 @@ static QVariant fcnLog( const QVariantList &values, const QgsExpressionContext *
}
static QVariant fcnRndF( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
double min = QgsExpressionUtils::getDoubleValue( values.at( 0 ), parent );
double max = QgsExpressionUtils::getDoubleValue( values.at( 1 ), parent );

double min = QgsExpressionUtils::getIntValue( values.at( 0 ), parent );
double max = QgsExpressionUtils::getIntValue( values.at( 1 ), parent );
if ( max < min )
return QVariant();

QRandomGenerator *generator;
if ( QgsExpressionUtils::isNull( values.at( 2 ) ) )
{
generator = QRandomGenerator::global();
}
else
std::random_device rd;
std::mt19937_64 generator(rd());
std::uniform_real_distribution<double> dist(min, max);

if ( !QgsExpressionUtils::isNull( values.at( 2 ) ) )
{
quint32 seed;
if ( QgsExpressionUtils::isIntSafe( values.at( 2 ) ) )
Expand All @@ -392,11 +393,11 @@ static QVariant fcnRndF( const QVariantList &values, const QgsExpressionContext
std::hash<std::string> hasher;
seed = hasher( seedStr.toStdString() );
}
generator = &QRandomGenerator::QRandomGenerator( seed );
generator.seed( seed );
}

// Return a random double in the range [min, max] (inclusive)
return QVariant( min + generator->generateDouble() * ( max - min ) );
// Return a random integer in the range [min, max] (inclusive)
return QVariant( dist(generator) );
}
static QVariant fcnRnd( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
Expand All @@ -405,12 +406,11 @@ static QVariant fcnRnd( const QVariantList &values, const QgsExpressionContext *
if ( max < min )
return QVariant();

QRandomGenerator *generator;
if ( QgsExpressionUtils::isNull( values.at( 2 ) ) )
{
generator = QRandomGenerator::global();
}
else
std::random_device rd;
std::mt19937_64 generator(rd());
std::uniform_int_distribution<qlonglong> dist(min, max);

if ( !QgsExpressionUtils::isNull( values.at( 2 ) ) )
{
quint32 seed;
if ( QgsExpressionUtils::isIntSafe( values.at( 2 ) ) )
Expand All @@ -425,11 +425,11 @@ static QVariant fcnRnd( const QVariantList &values, const QgsExpressionContext *
std::hash<std::string> hasher;
seed = hasher( seedStr.toStdString() );
}
generator = &QRandomGenerator::QRandomGenerator( seed );
generator.seed( seed );
}

// Return a random integer in the range [min, max] (inclusive)
return QVariant( min + static_cast< qlonglong >(generator->bounded( static_cast< quint32 >(max - min + 1) ) ) );
return QVariant( dist(generator) );
}

static QVariant fcnLinearScale( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
Expand Down

0 comments on commit 800198b

Please sign in to comment.