Skip to content

6 moved definitions from Fortune.hpp file into Fortune.cpp source file #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions shm/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ project(SHM)

# set executables
set(THIS_PROJECT_SRC_DIRECTORIES
source/Fortune.cpp
source/Map.cpp
)
set(THIS_PROJECT_TESTS_DIRECTORIES
Expand Down
32 changes: 32 additions & 0 deletions shm/source/Fortune.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "Fortune.hpp"

std::random_device randomDevice;
std::mt19937 randomEngine(randomDevice());

int Fortune::getNumber(int first, int last) {
if (first > last) {
std::swap(first, last);
}
std::uniform_int_distribution<int> distribution(first, last);
return distribution(randomEngine);
}

std::vector<int> Fortune::getNumbersEvenlyDistributed(int first, int last, const int n) {
if (n < 1) {
return {};
}
if (first > last) {
std::swap(first, last);
}
std::vector<int> result(n);
for (int i = 0; i < n; ++i) {
int localFirst = (last / n * i) + first;
int localLast = (last / n * (i + 1)) + first;
result.at(i) = (getNumber(localFirst, localLast));
}
return result;
}

void Fortune::shuffle(std::vector<int>& vector) {
std::shuffle(vector.begin(), vector.end(), randomEngine);
}
44 changes: 6 additions & 38 deletions shm/source/Fortune.hpp
Original file line number Diff line number Diff line change
@@ -1,42 +1,10 @@
#pragma once
#include <exception>
#include <algorithm>
#include <random>
#include <stdexcept>
#include <vector>

struct Fortune {
static int getNumber(int first, int last) {
const int result = makeRandomNumber(first, last);
if (result < first || result > last) {
throw std::logic_error("Fortune::getNumber(int,int): Result value out of scope");
}
return result;
}

static std::vector<int> getNumbersEvenlyDistributed(int first, int last, const int n) {
if (n < 1) {
return {};
}
std::vector<int> result(n);
for (int i = 0; i < n; ++i) {
int localFirst = (last / n * i) + first;
int localLast = (last / n * (i + 1)) + first;
result.at(i) = (getNumber(localFirst, localLast));
}
return result;
}

static void shuffle(std::vector<int>& vector) {
std::random_device random_device;
std::mt19937 random_engine(random_device());
std::shuffle(vector.begin(), vector.end(), random_engine);
}

private:
static int makeRandomNumber(const int first, const int last) {
std::random_device random_device;
std::mt19937 random_engine(random_device());
std::uniform_int_distribution<int> distribution(first, last);
return distribution(random_engine);
}
};
namespace Fortune {
int getNumber(int first, int last);
std::vector<int> getNumbersEvenlyDistributed(int first, int last, const int n);
void shuffle(std::vector<int>& vector);
}