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
8#include "faker-cxx/export.h"
9#include "faker-cxx/types/hex.h"
10
12{
29template <std::integral I>
30I integer(I min, I max)
31{
32 if (min > max)
33 {
34 throw std::invalid_argument("Minimum value must be smaller than maximum value.");
35 }
36
37 static std::mt19937 pseudoRandomGenerator{std::random_device{}()};
38
39 std::uniform_int_distribution<I> distribution(min, max);
40
41 return distribution(pseudoRandomGenerator);
42}
43
60template <std::integral I>
61I integer(I max)
62{
63 return integer<I>(static_cast<I>(0), max);
64}
65
82template <std::floating_point F>
83F decimal(F min, F max)
84{
85 if (min > max)
86 {
87 throw std::invalid_argument("Minimum value must be smaller than maximum value.");
88 }
89
90 static std::mt19937 pseudoRandomGenerator{std::random_device{}()};
91 std::uniform_real_distribution<F> distribution(min, max);
92
93 return distribution(pseudoRandomGenerator);
94}
95
112template <std::floating_point F>
113F decimal(F max)
114{
115 return decimal<F>(static_cast<F>(0.), max);
116}
117
135template <std::floating_point F>
136F normalDistribution(F mean, F standardDeviation)
137{
138 if (standardDeviation < 0 || standardDeviation == INFINITY || mean == INFINITY)
139 {
140 throw std::invalid_argument("Standard Deviation cannot be negative");
141 }
142 else if (standardDeviation == 0)
143 {
144 return mean;
145 }
146
147 std::random_device randDev;
148 std::mt19937 PSRNG(randDev());
149
150 std::normal_distribution<F> dist(mean, standardDeviation);
151 return dist(PSRNG);
152}
153
175template <std::floating_point F>
176F normalDistribution(F mean, F standardDeviation, F min, F max)
177{
178 if (min > max)
179 {
180 throw std::invalid_argument("min cannot be larger than max");
181 }
182
183 F sample = normalDistribution(mean, standardDeviation);
184
185 if (sample > max)
186 {
187 sample = max;
188 }
189 else if (sample < min)
190 {
191 sample = min;
192 }
193
194 return sample;
195}
196
213FAKER_CXX_EXPORT std::string hexadecimal(unsigned length = 1, HexCasing casing = HexCasing::Lower,
214 HexPrefix prefix = HexPrefix::ZeroX);
215
229FAKER_CXX_EXPORT std::string hexadecimal(std::optional<int> min = std::nullopt, std::optional<int> max = std::nullopt);
230
242FAKER_CXX_EXPORT std::string octal(unsigned length = 1);
243
257FAKER_CXX_EXPORT std::string binary(int length = 1);
258
273FAKER_CXX_EXPORT std::string binary(int min, int max);
274}
Definition number.h:12
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:30
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 sample(unsigned length=10)
Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`).