Skip to content

Commit

Permalink
Use several random_device calls to initialize rnd
Browse files Browse the repository at this point in the history
  • Loading branch information
ifsmirnov committed Feb 6, 2018
1 parent e1e4d00 commit e582b80
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions doc/random.md
Original file line number Diff line number Diff line change
@@ -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*.

Expand All @@ -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 &lt; 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)
Expand Down
9 changes: 8 additions & 1 deletion jngen.h
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,14 @@ class Random {
Random() {
assertRandomEngineConsistency();
assertIntegerSizes();
seed(std::random_device{}());
std::vector<uint32_t> 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);
Expand Down
9 changes: 8 additions & 1 deletion random.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,14 @@ class Random {
Random() {
assertRandomEngineConsistency();
assertIntegerSizes();
seed(std::random_device{}());
std::vector<uint32_t> 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);
Expand Down

0 comments on commit e582b80

Please sign in to comment.