-
-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dario-loi/number_improvements
Number improvements
- Loading branch information
Showing
6 changed files
with
71 additions
and
38 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,60 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <stdexcept> | ||
#include <random> | ||
#include <concepts> | ||
|
||
namespace faker | ||
{ | ||
template <std::integral T> | ||
class Number | ||
{ | ||
|
||
private: | ||
static std::random_device randomDevice; | ||
static std::mt19937 pseudoRandomGenerator; | ||
|
||
|
||
public: | ||
static int integer(int max = 10000, int min = 0); | ||
|
||
/** | ||
* @brief Generates a random integer number in the given range, bounds included. | ||
* | ||
* @param min the minimum value of the range | ||
* @param max the maximum value of the range | ||
* @return T the type of the generated number, must be an integral type | ||
*/ | ||
static T integer(T min, T max) | ||
{ | ||
if (min > max) | ||
{ | ||
throw std::invalid_argument("Minimum value must be smaller than maximum value."); | ||
} | ||
|
||
std::uniform_int_distribution<T> distribution(min, max); | ||
|
||
return distribution(pseudoRandomGenerator); | ||
} | ||
|
||
/** | ||
* @brief Generates a random integer between 0 and the given maximum value, bounds included. | ||
* | ||
* @param max the maximum value of the range | ||
* @return T the type of the generated number, must be an integral type | ||
*/ | ||
static T integer(T max) | ||
{ | ||
return integer(0, max); | ||
} | ||
|
||
|
||
}; | ||
} | ||
|
||
template <std::integral T> | ||
std::random_device Number<T>::randomDevice; | ||
|
||
template <std::integral T> | ||
std::mt19937 Number<T>::pseudoRandomGenerator(Number<T>::randomDevice()); | ||
|
||
} |
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 was deleted.
Oops, something went wrong.
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