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"
10#include "faker-cxx/types/hex.h"
11
13{
30template <std::integral I>
31I integer(I min, I max)
32{
33 if (min > max)
34 {
35 throw std::invalid_argument("Minimum value must be smaller than maximum value.");
36 }
37
38 std::mt19937_64& pseudoRandomGenerator = getGenerator();
39
40 std::uniform_int_distribution<I> distribution(min, max);
41
42 return distribution(pseudoRandomGenerator);
43}
44
61template <std::integral I>
62I integer(I max)
63{
64 return integer<I>(static_cast<I>(0), max);
65}
66
83template <std::floating_point F>
84F decimal(F min, F max)
85{
86 if (min > max)
87 {
88 throw std::invalid_argument("Minimum value must be smaller than maximum value.");
89 }
90
91 std::mt19937_64& pseudoRandomGenerator = getGenerator();
92 std::uniform_real_distribution<F> distribution(min, max);
93
94 return distribution(pseudoRandomGenerator);
95}
96
113template <std::floating_point F>
114F decimal(F max)
115{
116 return decimal<F>(static_cast<F>(0.), max);
117}
118
135
136template <std::floating_point F>
137F normalDistribution(F mean, F standardDeviation)
138{
139 if (standardDeviation < 0 || standardDeviation == INFINITY || mean == INFINITY)
140 {
141 throw std::invalid_argument("Standard Deviation cannot be negative");
142 }
143 else if (standardDeviation == 0)
144 {
145 return mean;
146 }
147
148 std::mt19937_64& pseudoRandomGenerator = getGenerator();
149
150 std::normal_distribution<F> distribution(mean, standardDeviation);
151 return distribution(pseudoRandomGenerator);
152}
153
174
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:13
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:31
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 `}`).
FAKER_CXX_EXPORT std::mt19937_64 & getGenerator()