diff --git a/src/func.cc b/src/func.cc index eaaae746..20f487a7 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( evalctx->getArgValue(2).toDouble() ) ); boost::uniform_real<> distributor( min, max ); Value::VectorType vec; - for (int i=0; igetArgValue(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); } diff --git a/testdata/scad/functions/rands.scad b/testdata/scad/functions/rands.scad new file mode 100644 index 00000000..7733dbb4 --- /dev/null +++ b/testdata/scad/functions/rands.scad @@ -0,0 +1,35 @@ +v1 = rands(0,0,0,0); +v2 = rands(-10,0,20,0); +v3 = rands(0,0,-20); +v4 = rands(1,2,-20); +v5 = rands(-1,-2,-20); +v6 = rands(-2,-1,-20); +v7 = rands(0,2,0,0); +v8 = rands(0,-3,-10,0); +v9 = rands(-40,-3,1000,0); +va = rands(10,200,1000,-32); +vb = rands("akhma","to","va"); + +vc = rands(0,0,-20); +vd = rands(1,2,-20); +ve = rands(-10,0,20); +vf = rands(0,2,0); +vg = rands(0,-3,0); +vh = rands(1,0,"blah"); +vi = rands(0,-3,-10); +vj = rands(-40,-3,1000); +vk = rands(10,200,1000); + +vq = rands(1, 5); +vp = rands(1, 5); +vo = rands(1); +vn = rands(); +vl = rands(v[0],v[1],v[2]); +vm = rands(1,2,-20); + +sphere(); +echo("i hope rands() did not crash"); + +v = rands(1,1,4); + +echo( v ); \ No newline at end of file