Faker C++
Loading...
Searching...
No Matches
faker::number Namespace Reference

Functions

template<std::integral I>
integer (I min, I max)
 Generates a random integer number in the given range, bounds included.
 
template<std::integral I>
integer (I max)
 Generates a random integer between 0 and the given maximum value, bounds included.
 
FAKER_CXX_EXPORT std::string hexadecimal (unsigned length=1, HexCasing casing=HexCasing::Lower, HexPrefix prefix=HexPrefix::ZeroX)
 Generates a random decimal number in the given range, bounds included.
 
FAKER_CXX_EXPORT std::string hexadecimal (std::optional< int > min=std::nullopt, std::optional< int > max=std::nullopt)
 Returns a lowercase hexadecimal number.
 
FAKER_CXX_EXPORT std::string octal (unsigned length=1)
 Generates an octal string.
 
FAKER_CXX_EXPORT std::string binary (int length=1)
 Generates a binary string of a specified length.
 
FAKER_CXX_EXPORT std::string binary (int min, int max)
 Generates a random binary string which has its decimal equivalent between min and max inclusive.
 

Function Documentation

◆ binary() [1/2]

FAKER_CXX_EXPORT std::string faker::number::binary ( int length = 1)

Generates a binary string of a specified length.

Parameters
lengthThe number of digits to generate. Defaults to `1`.
Returns
Binary string.
Exceptions
std::invalid_argument,iflength is negative
faker::string::binary(8) // "0b01110101"

◆ binary() [2/2]

FAKER_CXX_EXPORT std::string faker::number::binary ( int min,
int max )

Generates a random binary string which has its decimal equivalent between min and max inclusive.

Parameters
minthe minimum possible decimal equivalent of the output
maxthe maximum possible decimal equivalent of the output
Returns
Binary string.
Exceptions
std::invalid_argument,ifmin > max, std::invalid_argument if min or max are negative
faker::string::binary(0, 1024) // "0b10110"

◆ hexadecimal() [1/2]

FAKER_CXX_EXPORT std::string faker::number::hexadecimal ( std::optional< int > min = std::nullopt,
std::optional< int > max = std::nullopt )

Returns a lowercase hexadecimal number.

Parameters
minOptional parameter for lower bound of generated number.
maxOptional parameter for upper bound of generated number.
Returns
A lowercase hexadecimal number.
faker::string::hexadecimal() // "b"
faker::string::hexadecimal(0, 255) // "9d"
FAKER_CXX_EXPORT std::string hexadecimal(unsigned length=1, HexCasing casing=HexCasing::Lower, HexPrefix prefix=HexPrefix::ZeroX)
Generates a random decimal number in the given range, bounds included.
Definition airline.h:9

◆ hexadecimal() [2/2]

FAKER_CXX_EXPORT std::string faker::number::hexadecimal ( unsigned length = 1,
HexCasing casing = HexCasing::Lower,
HexPrefix prefix = HexPrefix::ZeroX )

Generates a random decimal number in the given range, bounds included.

Template Parameters
Fthe type of the generated number, must be a floating point type (float, double, long double).
Parameters
minThe minimum value of the range.
maxThe maximum value of the range.
Exceptions
std::invalid_argumentif min is greater than max.
Returns
F a random decimal number.
faker::number::decimal(10.2, 17.7) // 15.6
@encode
/
template <std::floating_point F>
F decimal(F min, F max)
{
if (min > max)
{
throw std::invalid_argument("Minimum value must be smaller than maximum value.");
}
static std::mt19937 pseudoRandomGenerator{std::random_device{}()};
std::uniform_real_distribution<F> distribution(min, max);
return distribution(pseudoRandomGenerator);
}
template <std::floating_point F>
F decimal(F max)
{
return decimal<F>(static_cast<F>(0.), max);
}
template <std::floating_point F>
F normalDistribution(F mean, F standardDeviation)
{
if (standardDeviation < 0 || standardDeviation == INFINITY || mean == INFINITY)
{
throw std::invalid_argument("Standard Deviation cannot be negative");
}
else if (standardDeviation == 0)
{
return mean;
}
std::random_device randDev;
std::mt19937 PSRNG(randDev());
std::normal_distribution<F> dist(mean, standardDeviation);
return dist(PSRNG);
}
template <std::floating_point F>
F normalDistribution(F mean, F standardDeviation, F min, F max)
{
if (min > max)
{
throw std::invalid_argument("min cannot be larger than max");
}
F sample = normalDistribution(mean, standardDeviation);
if (sample > max)
{
sample = max;
}
else if (sample < min)
{
sample = min;
}
return sample;
}
FAKER_CXX_EXPORT std::string sample(unsigned length=10)
Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`).

◆ integer() [1/2]

template<std::integral I>
I faker::number::integer ( I max)

Generates a random integer between 0 and the given maximum value, bounds included.

Template Parameters
Ithe type of the generated number, must be an integral type (int, long, long long, etc.).
Parameters
maxthe maximum value of the range.
Exceptions
std::invalid_argumentif min is greater than max.
See also
integer<I>(I)
Returns
T a random integer number
I integer(I min, I max)
Generates a random integer number in the given range, bounds included.
Definition number.h:31

◆ integer() [2/2]

template<std::integral I>
I faker::number::integer ( I min,
I max )

Generates a random integer number in the given range, bounds included.

Parameters
minThe minimum value of the range.
maxThe maximum value of the range.
Template Parameters
Ithe type of the generated number, must be an integral type (int, long, long long, etc.).
Exceptions
std::invalid_argumentif min is greater than max.
Returns
T a random integer number

◆ octal()

FAKER_CXX_EXPORT std::string faker::number::octal ( unsigned length = 1)

Generates an octal string.

Parameters
lengthThe number of digits to generate. Defaults to `1`.
Returns
Octal string.
faker::string::octal(8) // "0o52561721"