mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-07-26 17:52:50 +00:00
Make Math.random() more deterministic
This was SVN commit r7563.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user