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{
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 static std::mt19937 pseudoRandomGenerator{std::random_device{}()};
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 static std::mt19937 pseudoRandomGenerator{std::random_device{}()};
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
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::random_device randDev;
149 std::mt19937 PSRNG(randDev());
150
151 std::normal_distribution<F> dist(mean, standardDeviation);
152 return dist(PSRNG);
153}
154
176template <std::floating_point F>
177F normalDistribution(F mean, F standardDeviation, F min, F max)
178{
179 if (min > max)
180 {
181 throw std::invalid_argument("min cannot be larger than max");
182 }
183
184 F sample = normalDistribution(mean, standardDeviation);
185
186 if (sample > max)
187 {
188 sample = max;
189 }
190 else if (sample < min)
191 {
192 sample = min;
193 }
194
195 return sample;
196}
197
214FAKER_CXX_EXPORT std::string hexadecimal(unsigned length = 1, HexCasing casing = HexCasing::Lower,
215 HexPrefix prefix = HexPrefix::ZeroX);
216
230FAKER_CXX_EXPORT std::string hexadecimal(std::optional<int> min = std::nullopt, std::optional<int> max = std::nullopt);
231
243FAKER_CXX_EXPORT std::string octal(unsigned length = 1);
244
258FAKER_CXX_EXPORT std::string binary(int length = 1);
259
274FAKER_CXX_EXPORT std::string binary(int min, int max);
275}
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: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 `}`).