From f40b6a672aa4c0a3df2165ffaaac49b9a8d935e7 Mon Sep 17 00:00:00 2001 From: don bright Date: Sun, 6 Jan 2013 06:27:42 +0100 Subject: switch builtin_rands() to use boost::random per issue 234 diff --git a/src/func.cc b/src/func.cc index e427bf2..a4d48e2 100644 --- a/src/func.cc +++ b/src/func.cc @@ -34,6 +34,14 @@ #include #include "stl-utils.h" #include "printutils.h" +#include +#include +#include + +boost::random::random_device nondeterministic_rng; +boost::random::mt19937 deterministic_rng; + +#include "random_device.cpp" AbstractFunction::~AbstractFunction() { @@ -131,12 +139,13 @@ double frand(double min, double max) Value builtin_rands(const Context *, const std::vector&, const std::vector &args) { + bool deterministic = false; if (args.size() == 3 && args[0].type() == Value::NUMBER && args[1].type() == Value::NUMBER && args[2].type() == Value::NUMBER) { - srand((unsigned int)time(0)); + deterministic = false; } else if (args.size() == 4 && args[0].type() == Value::NUMBER && @@ -144,7 +153,8 @@ Value builtin_rands(const Context *, const std::vector&, const std: args[2].type() == Value::NUMBER && args[3].type() == Value::NUMBER) { - srand((unsigned int)args[3].toDouble()); + deterministic_rng.seed( (unsigned int) args[3].toDouble() ); + deterministic = true; } else { @@ -153,7 +163,14 @@ Value builtin_rands(const Context *, const std::vector&, const std: Value::VectorType vec; for (int i=0; i dist( min, max ); + if ( deterministic ) { + vec.push_back( Value( dist( deterministic_rng ) ) ); + } else { + vec.push_back( Value( dist( nondeterministic_rng ) ) ); + } } return Value(vec); -- cgit v0.10.1