From 75bfb758822cc926a1852c6a86a7ce5d153a1320 Mon Sep 17 00:00:00 2001 From: Jeroen Vermeulen Date: Wed, 22 Apr 2015 20:43:29 +0700 Subject: Thread-safe, platform-agnostic randomizer. Some places in mert use srandom()/random(), but these are POSIX-specific. The standard alternative, srand()/rand(), is not thread-safe. This module wraps srand()/rand() in mutexes (very short-lived, so should not cost much) so that it relies on just Boost and the C standard library, not on a Unix-like environment. This may reduce the width of the random numbers on some platforms: it goes from "long int" to just "int". If that is a problem, we may have to use Boost's randomizer utilities, or eventually, the C++ ones. --- mert/Data.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'mert/Data.cpp') diff --git a/mert/Data.cpp b/mert/Data.cpp index 49c1239e5..428886b99 100644 --- a/mert/Data.cpp +++ b/mert/Data.cpp @@ -17,6 +17,7 @@ #include "util/exception.hh" #include "util/file_piece.hh" +#include "util/random.hh" #include "util/tokenize_piece.hh" #include "util/string_piece.hh" #include "FeatureDataIterator.h" @@ -286,7 +287,7 @@ void Data::createShards(size_t shard_count, float shard_size, const string& scor } else { //create shards by randomly sampling for (size_t i = 0; i < floor(shard_size+0.5); ++i) { - shard_contents.push_back(rand() % data_size); + shard_contents.push_back(util::rand_int() % data_size); } } -- cgit v1.2.3