diff options
author | Don Bright <hugh.m.bright@gmail.com> | 2013-08-11 14:54:04 (GMT) |
---|---|---|
committer | Don Bright <hugh.m.bright@gmail.com> | 2013-08-11 14:54:04 (GMT) |
commit | d2933590c23ae3a3d95bd1d2d74d03cdbb7897cc (patch) | |
tree | 0830ec04e440197b60632c838cc341b24f5c0279 /src/func.cc | |
parent | 6900160b7d67f2b572b5b3f9d1568904acf9d52f (diff) |
fix #452
Diffstat (limited to 'src/func.cc')
-rw-r--r-- | src/func.cc | 27 |
1 files changed, 16 insertions, 11 deletions
diff --git a/src/func.cc b/src/func.cc index eaaae74..20f487a 100644 --- a/src/func.cc +++ b/src/func.cc @@ -153,10 +153,10 @@ Value builtin_rands(const Context *, const EvalContext *evalctx) deterministic = false; } else if (evalctx->numArgs() == 4 && - evalctx->getArgValue(0).type() == Value::NUMBER && - evalctx->getArgValue(1).type() == Value::NUMBER && - evalctx->getArgValue(2).type() == Value::NUMBER && - evalctx->getArgValue(3).type() == Value::NUMBER) + evalctx->getArgValue(0).type() == Value::NUMBER && + evalctx->getArgValue(1).type() == Value::NUMBER && + evalctx->getArgValue(2).type() == Value::NUMBER && + evalctx->getArgValue(3).type() == Value::NUMBER) { deterministic_rng.seed( (unsigned int) evalctx->getArgValue(3).toDouble() ); deterministic = true; @@ -165,19 +165,24 @@ Value builtin_rands(const Context *, const EvalContext *evalctx) { return Value(); } - + double min = std::min( evalctx->getArgValue(0).toDouble(), evalctx->getArgValue(1).toDouble() ); double max = std::max( evalctx->getArgValue(0).toDouble(), evalctx->getArgValue(1).toDouble() ); + size_t numresults = std::max( 0, static_cast<int>( evalctx->getArgValue(2).toDouble() ) ); boost::uniform_real<> distributor( min, max ); Value::VectorType vec; - for (int i=0; i<evalctx->getArgValue(2).toDouble(); i++) { - if ( deterministic ) { - vec.push_back( Value( distributor( deterministic_rng ) ) ); - } else { - vec.push_back( Value( distributor( lessdeterministic_rng ) ) ); + if (min==max) { // workaround boost bug + for (size_t i=0; i < numresults; i++) + vec.push_back( Value( min ) ); + } else { + for (size_t i=0; i < numresults; i++) { + if ( deterministic ) { + vec.push_back( Value( distributor( deterministic_rng ) ) ); + } else { + vec.push_back( Value( distributor( lessdeterministic_rng ) ) ); + } } } - return Value(vec); } |