summaryrefslogtreecommitdiff
path: root/src/func.cc
diff options
context:
space:
mode:
authordon bright <hugh.m.bright@gmail.com>2013-01-06 05:27:42 (GMT)
committerdon bright <hugh.m.bright@gmail.com>2013-01-06 05:27:42 (GMT)
commitf40b6a672aa4c0a3df2165ffaaac49b9a8d935e7 (patch)
tree7c7106962ea59f4bb7b305d7291f5b46c2c66b55 /src/func.cc
parent101510c5cb3f696ee01fc900c9269d87a09c0658 (diff)
switch builtin_rands() to use boost::random per issue 234
Diffstat (limited to 'src/func.cc')
-rw-r--r--src/func.cc23
1 files changed, 20 insertions, 3 deletions
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 <algorithm>
#include "stl-utils.h"
#include "printutils.h"
+#include <boost/random/random_device.hpp>
+#include <boost/random/mersenne_twister.hpp>
+#include <boost/random/uniform_real_distribution.hpp>
+
+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<std::string>&, const std::vector<Value> &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<std::string>&, 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<std::string>&, const std:
Value::VectorType vec;
for (int i=0; i<args[2].toDouble(); i++) {
- vec.push_back(Value(frand(args[0].toDouble(), args[1].toDouble())));
+ double min = std::min( args[0].toDouble(), args[1].toDouble() );
+ double max = std::max( args[0].toDouble(), args[1].toDouble() );
+ boost::random::uniform_real_distribution<> dist( min, max );
+ if ( deterministic ) {
+ vec.push_back( Value( dist( deterministic_rng ) ) );
+ } else {
+ vec.push_back( Value( dist( nondeterministic_rng ) ) );
+ }
}
return Value(vec);
contact: Jan Huwald // Impressum