Make Math.random() more deterministic

This was SVN commit r7563.
This commit is contained in:
Ykkrosh
2010-05-22 00:38:33 +00:00
parent 1fc75e7605
commit 386f322b1c
6 changed files with 95 additions and 0 deletions
@@ -21,6 +21,8 @@
#include "ps/CLogger.h"
#include <boost/random/linear_congruential.hpp>
class TestScriptInterface : public CxxTest::TestSuite
{
public:
@@ -103,4 +105,24 @@ public:
TS_ASSERT(script2.CallFunction(obj2.get(), "toSource", source));
TS_ASSERT_STR_EQUALS(source, "({a:#1=[#1#], b:#1#})");
}
void test_random()
{
ScriptInterface script("Test");
double d1, d2;
TS_ASSERT(script.Eval("Math.random()", d1));
TS_ASSERT(script.Eval("Math.random()", d2));
TS_ASSERT_DIFFERS(d1, d2);
boost::rand48 rng;
script.ReplaceNondeterministicFunctions(rng);
rng.seed(0);
TS_ASSERT(script.Eval("Math.random()", d1));
TS_ASSERT(script.Eval("Math.random()", d2));
TS_ASSERT_DIFFERS(d1, d2);
rng.seed(0);
TS_ASSERT(script.Eval("Math.random()", d2));
TS_ASSERT_EQUALS(d1, d2);
}
};