From e582b80c900f66fefcd97b70425cce2222857979 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Tue, 6 Feb 2018 20:24:49 +0300 Subject: [PATCH] Use several random_device calls to initialize rnd --- doc/random.md | 4 ++-- jngen.h | 9 ++++++++- random.h | 9 ++++++++- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/doc/random.md b/doc/random.md index ba1bfda..b157abc 100644 --- a/doc/random.md +++ b/doc/random.md @@ -1,6 +1,6 @@ ## Random numbers generation -Jngen provides a class *Random* whose behavior is similar to *rnd* from testlib.h. E.g. you may write *rnd.next(100)*, *rnd.next("[a-z]{%d}, n)*, and so on. Most of interaction with *Random* happens via its global instance of *Random* called *rnd*. +Jngen provides a class *Random* whose behavior is similar to *rnd* from testlib.h. E.g. you may write *rnd.next(100)*, *rnd.next("[a-z]{%d}", n)*, and so on. Most of interaction with *Random* happens via its global instance of *Random* called *rnd*. Default initialized *Random* is seeded with some hardware-generated random value, so subsequent executions of the program will produce different tests. This may be useful for local stress-testing, for example. If you want to fix the seed, use *registerGen(argc, argv)* at the beginning of your *main*. @@ -17,7 +17,7 @@ Default initialized *Random* is seeded with some hardware-generated random value #### int next(int l, int r) // also for long long, size\_t, double * Returns: random integer in range [l, r]. #### int wnext(int n, int w) // also for long long, size\_t, double -* If w > 0, returns max(next(n), ..., next(n)) (w times). If w < 0, returns min(next(n), ..., next(n)) (-w times). If w = 0, same as next(n). +* If w > 0, returns max(next(n), ..., next(n)) (w times). If w < 0, returns min(next(n), ..., next(n)) (-w times). If w = 0, same as next(n). #### int wnext(int l, int r, int w) // also for long long, size\_t, double * Same as wnext(n, w), but the range is [l, r]. #### std::string next(const std::string& pattern) diff --git a/jngen.h b/jngen.h index 65d9ad5..dc3dddb 100644 --- a/jngen.h +++ b/jngen.h @@ -1661,7 +1661,14 @@ class Random { Random() { assertRandomEngineConsistency(); assertIntegerSizes(); - seed(std::random_device{}()); + std::vector seedSeq; + // 4 random_device calls is enough for everyone + std::random_device rd; + for (size_t i = 0; i < 4; ++i) { + seedSeq.push_back(rd()); + } + seed(seedSeq); + } void seed(uint32_t val); diff --git a/random.h b/random.h index 239f87c..8e741b7 100644 --- a/random.h +++ b/random.h @@ -57,7 +57,14 @@ class Random { Random() { assertRandomEngineConsistency(); assertIntegerSizes(); - seed(std::random_device{}()); + std::vector seedSeq; + // 4 random_device calls is enough for everyone + std::random_device rd; + for (size_t i = 0; i < 4; ++i) { + seedSeq.push_back(rd()); + } + seed(seedSeq); + } void seed(uint32_t val);