-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGenerator.cpp
More file actions
33 lines (26 loc) · 859 Bytes
/
NumberGenerator.cpp
File metadata and controls
33 lines (26 loc) · 859 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//
// Created by Daria Kuznetsova on 31.10.2024.
//
#include "NumberGenerator.h"
#include "random"
std::random_device NumberGenerator::rand;
std::mt19937 NumberGenerator::gen(NumberGenerator::rand());
// Generates a random number within the specified range [min, max].
size_t NumberGenerator::generateRandomNumber(size_t min, size_t max) {
std::uniform_int_distribution<> dis(static_cast<int>(min), static_cast<int>(max));
return dis(gen);
}
// Generates a random odd number within the specified range [min, max].
size_t NumberGenerator::generateRandomOddNumber(size_t min, size_t max) {
if (min % 2 == 0) {
min++;
}
if (max % 2 == 0) {
max--;
}
if (min > max) {
std::swap(min, max);
}
std::uniform_int_distribution<> dis(0, static_cast<int>(max - min) / 2);
return dis(gen) * 2 + min;
}