diff options
author | Jeroen Vermeulen <jtv@precisiontranslationtools.com> | 2015-04-23 15:27:21 +0300 |
---|---|---|
committer | Jeroen Vermeulen <jtv@precisiontranslationtools.com> | 2015-04-23 19:46:04 +0300 |
commit | 38d790cac0f21de9ff8951dce0bb5d522ad46c0d (patch) | |
tree | a65df558350c77e148e3c2fc6e40efa1ed6367e4 /mert/evaluator.cpp | |
parent | 4b47e1148c7cfe771c8e813cb9d741c2de44ed42 (diff) |
Add cross-platform randomizer module.
The code uses two mechanisms for generating random numbers: srand()/rand(),
which is not thread-safe, and srandom()/random(), which is POSIX-specific.
Here I add a util/random.cc module that centralizes these calls, and unifies
some common usage patterns. If the implementation is not good enough, we can
now change it in a single place.
To keep things simple, this uses the portable srand()/rand() but protects them
with a lock to avoid concurrency problems.
The hard part was to keep the regression tests passing: they rely on fixed
sequences of random numbers, so a small code change could break them very
thoroughly. Util::rand(), for wide types like size_t, calls std::rand() not
once but twice. This behaviour was generalized into utils::wide_rand() and
friends.
Diffstat (limited to 'mert/evaluator.cpp')
-rw-r--r-- | mert/evaluator.cpp | 6 |
1 files changed, 3 insertions, 3 deletions
diff --git a/mert/evaluator.cpp b/mert/evaluator.cpp index 61775a354..59ffaf3cd 100644 --- a/mert/evaluator.cpp +++ b/mert/evaluator.cpp @@ -95,7 +95,7 @@ void EvaluatorUtil::evaluate(const string& candFile, int bootstrap, bool nbest_i for (int i = 0; i < bootstrap; ++i) { ScoreData scoredata(g_scorer); for (int j = 0; j < n; ++j) { - int randomIndex = util::rand_int() % n; + const int randomIndex = util::rand_excl(n); scoredata.add(entries[randomIndex], j); } g_scorer->setScoreData(&scoredata); @@ -285,10 +285,10 @@ void InitSeed(const ProgramOption *opt) { if (opt->has_seed) { cerr << "Seeding random numbers with " << opt->seed << endl; - util::rand_int_init(opt->seed); + util::rand_init(opt->seed); } else { cerr << "Seeding random numbers with system clock " << endl; - util::rand_int_init(); + util::rand_init(); } } |