Faker C++
Loading...
Searching...
No Matches
number.h
Go to the documentation of this file.
1#pragma once
2
3#include <concepts>
4#include <optional>
5#include <random>
6#include <stdexcept>
7#include <type_traits>
8
9#include "faker-cxx/export.h"
10#include "faker-cxx/generator.h"
11#include "faker-cxx/types/hex.h"
12
14{
31template <std::integral I>
32 requires(sizeof(I) <= sizeof(long long))
33I integer(I min, I max)
34{
35 // std::uniform_int_distribution only accepts certain types, so we use signed or unsigned long long and don't allow
36 // larger types
37 using LongLongType = std::conditional_t<std::is_unsigned_v<I>, unsigned long long, long long>;
38
39 if (min > max)
40 {
41 throw std::invalid_argument("Minimum value must be smaller than maximum value.");
42 }
43
44 std::mt19937_64& pseudoRandomGenerator = getGenerator();
45
46 std::uniform_int_distribution<LongLongType> distribution(min, max);
47
48 return static_cast<I>(distribution(pseudoRandomGenerator));
49}
50
67template <std::integral I>
68 requires(sizeof(I) <= sizeof(long long))
69I integer(I max)
70{
71 return integer<I>(static_cast<I>(0), max);
72}
73
90template <std::floating_point F>
91F decimal(F min, F max)
92{
93 if (min > max)
94 {
95 throw std::invalid_argument("Minimum value must be smaller than maximum value.");
96 }
97
98 std::mt19937_64& pseudoRandomGenerator = getGenerator();
99 std::uniform_real_distribution<F> distribution(min, max);
100
101 return distribution(pseudoRandomGenerator);
102}
103
120template <std::floating_point F>
121F decimal(F max)
122{
123 return decimal<F>(static_cast<F>(0.), max);
124}
125
142
143template <std::floating_point F>
144F normalDistribution(F mean, F standardDeviation)
145{
146 if (standardDeviation < 0 || standardDeviation == INFINITY || mean == INFINITY)
147 {
148 throw std::invalid_argument("Standard Deviation cannot be negative");
149 }
150 else if (standardDeviation == 0)
151 {
152 return mean;
153 }
154
155 std::mt19937_64& pseudoRandomGenerator = getGenerator();
156
157 std::normal_distribution<F> distribution(mean, standardDeviation);
158 return distribution(pseudoRandomGenerator);
159}
160
181
182template <std::floating_point F>
183F normalDistribution(F mean, F standardDeviation, F min, F max)
184{
185 if (min > max)
186 {
187 throw std::invalid_argument("min cannot be larger than max");
188 }
189
190 F sample = normalDistribution(mean, standardDeviation);
191
192 if (sample > max)
193 {
194 sample = max;
195 }
196 else if (sample < min)
197 {
198 sample = min;
199 }
200
201 return sample;
202}
203
220FAKER_CXX_EXPORT std::string hexadecimal(unsigned length = 1, HexCasing casing = HexCasing::Lower,
221 HexPrefix prefix = HexPrefix::ZeroX);
222
236FAKER_CXX_EXPORT std::string hexadecimal(std::optional<int> min = std::nullopt, std::optional<int> max = std::nullopt);
237
249FAKER_CXX_EXPORT std::string octal(unsigned length = 1);
250
264FAKER_CXX_EXPORT std::string binary(int length = 1);
265
280FAKER_CXX_EXPORT std::string binary(int min, int max);
281
303FAKER_CXX_EXPORT std::string roman(std::optional<int> min = std::nullopt, std::optional<int> max = std::nullopt);
304
305}
Definition number.h:14
FAKER_CXX_EXPORT std::string octal(unsigned length=1)
Generates an octal string.
I integer(I min, I max)
Generates a random integer number in the given range, bounds included.
Definition number.h:33
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 binary(int length=1)
Generates a binary string of a specified length.
FAKER_CXX_EXPORT std::string roman(std::optional< int > min=std::nullopt, std::optional< int > max=std::nullopt)
Returns a roman numeral in String format.
FAKER_CXX_EXPORT std::string sample(unsigned length=10)
Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`).
FAKER_CXX_EXPORT std::mt19937_64 & getGenerator()