-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
I used "A Tour of C++, Third Edition" (ISBN-10 0136816487) as inspiration for the Random class. This replaces all occurrences of `rand()` and `srand()`. Co-authored-by: John Ericson <[email protected]> Co-authored-by: Eelco Dolstra <[email protected]>
- Loading branch information
1 parent
5e5c979
commit 94cb5a7
Showing
11 changed files
with
79 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#pragma once | ||
|
||
#include <random> | ||
#include <limits> | ||
|
||
namespace nix { | ||
|
||
// Inspired by the book "A Tour of C++, Third Edition" (ISBN-10 0136816487) | ||
template<typename T, typename Distribution, typename Engine> | ||
struct RandomNumberGenerator | ||
{ | ||
public: | ||
using limits = std::numeric_limits<T>; | ||
RandomNumberGenerator(T low = limits::min(), T high = limits::max()) | ||
: engine(std::random_device{}()) | ||
, dist(low, high){}; | ||
RandomNumberGenerator(std::seed_seq seed, T low, T high) | ||
: engine(seed) | ||
, dist(low, high){}; | ||
T operator()() | ||
{ | ||
return dist(engine); | ||
} | ||
void seed(int s) | ||
{ | ||
engine.seed(s); | ||
} | ||
private: | ||
Engine engine; | ||
Distribution dist; | ||
}; | ||
|
||
using RandomIntGenerator = RandomNumberGenerator<int, std::uniform_int_distribution<int>, std::default_random_engine>; | ||
using RandomFloatGenerator = | ||
RandomNumberGenerator<float, std::uniform_real_distribution<float>, std::default_random_engine>; | ||
|
||
} // namespace nix |