Skip to content

Commit

Permalink
followup: do not use uniform_real_distribution as it's platform depen…
Browse files Browse the repository at this point in the history
…dant
  • Loading branch information
olivierdalang committed Dec 13, 2019
1 parent adf40e2 commit 65112f8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
7 changes: 3 additions & 4 deletions src/core/expression/qgsexpressionfunction.cpp
Expand Up @@ -376,7 +376,6 @@ static QVariant fcnRndF( const QVariantList &values, const QgsExpressionContext

std::random_device rd;
std::mt19937_64 generator( rd() );
std::uniform_real_distribution<double> dist( min, max );

if ( !QgsExpressionUtils::isNull( values.at( 2 ) ) )
{
Expand All @@ -397,7 +396,8 @@ static QVariant fcnRndF( const QVariantList &values, const QgsExpressionContext
}

// Return a random integer in the range [min, max] (inclusive)
return QVariant( dist( generator ) );
double f = static_cast< double >( generator() ) / generator.max();
return QVariant( min + f * ( max - min ) );
}
static QVariant fcnRnd( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
{
Expand All @@ -408,7 +408,6 @@ static QVariant fcnRnd( const QVariantList &values, const QgsExpressionContext *

std::random_device rd;
std::mt19937_64 generator( rd() );
std::uniform_int_distribution<qlonglong> dist( min, max );

if ( !QgsExpressionUtils::isNull( values.at( 2 ) ) )
{
Expand All @@ -429,7 +428,7 @@ static QVariant fcnRnd( const QVariantList &values, const QgsExpressionContext *
}

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

static QVariant fcnLinearScale( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
Expand Down
26 changes: 10 additions & 16 deletions tests/src/core/testqgsexpression.cpp
Expand Up @@ -2179,20 +2179,17 @@ class TestQgsExpression: public QObject
QCOMPARE( v7.toInt() <= 10, true );
QCOMPARE( v7.toInt() >= 1, true );

// Two calls with the same seed return the same number
// Two calls with the same seed always return the same number
QgsExpression exp8( QStringLiteral( "rand(1,1000000000,1)" ) );
QVariant v8 = exp8.evaluate();
QCOMPARE( v8.toInt() == 546311529, true );
QgsExpression exp9( QStringLiteral( "rand(1,1000000000,1)" ) );
QVariant v9 = exp9.evaluate();
QCOMPARE( v9.toInt() == 546311529, true );

// Two calls with a different seed return a different number
QgsExpression exp10( QStringLiteral( "rand(1,100000000000,1)" ) );
QgsExpression exp9( QStringLiteral( "rand(1,100000000000,1)" ) );
QVariant v9 = exp9.evaluate();
QgsExpression exp10( QStringLiteral( "rand(1,100000000000,2)" ) );
QVariant v10 = exp10.evaluate();
QgsExpression exp11( QStringLiteral( "rand(1,100000000000,2)" ) );
QVariant v11 = exp11.evaluate();
QCOMPARE( v10.toInt() != v11.toInt(), true );
QCOMPARE( v9.toInt() != v10.toInt(), true );
}

void eval_randf()
Expand Down Expand Up @@ -2229,20 +2226,17 @@ class TestQgsExpression: public QObject
QCOMPARE( v7.toDouble() <= 9.5, true );
QCOMPARE( v7.toDouble() >= 1.5, true );

// Two calls with the same seed return the same number
// Two calls with the same seed always return the same number
QgsExpression exp8( QStringLiteral( "randf(seed:=1)" ) );
QVariant v8 = exp8.evaluate();
QCOMPARE( v8.toFloat() == 0.13387664401253274, true );
QgsExpression exp9( QStringLiteral( "randf(seed:=1)" ) );
QVariant v9 = exp9.evaluate();
QCOMPARE( v9.toFloat() == 0.13387664401253274, true );

// Two calls with a different seed return a different number
QgsExpression exp10( QStringLiteral( "randf(seed:=1)" ) );
QgsExpression exp9( QStringLiteral( "randf(seed:=1)" ) );
QVariant v9 = exp9.evaluate();
QgsExpression exp10( QStringLiteral( "randf(seed:=2)" ) );
QVariant v10 = exp10.evaluate();
QgsExpression exp11( QStringLiteral( "randf(seed:=2)" ) );
QVariant v11 = exp11.evaluate();
QCOMPARE( v10.toFloat() != v11.toFloat(), true );
QCOMPARE( v9.toFloat() != v10.toFloat(), true );
}

void referenced_columns()
Expand Down

0 comments on commit 65112f8

Please sign in to comment.