Это мой код для использования Mersenne
Код: Выделить всё
#include
#include
#include
#include
struct Element {
int value;
std::string attribute1;
std::string attribute2;
std::string attribute3;
std::string attribute4;
std::string attribute5;
int count;
};
bool haveSameAttributes(const Element& elem1, const Element& elem2) {
return (elem1.attribute1 == elem2.attribute1 &&
elem1.attribute2 == elem2.attribute2 &&
elem1.attribute3 == elem2.attribute3 &&
elem1.attribute4 == elem2.attribute4 &&
elem1.attribute5 == elem2.attribute5);
}
void enhancedShuffle(std::vector& rndnum, std::mt19937& gen) {
int n = rndnum.size();
int n1 = n;
int j = n - 1;
int attempts = 0;
const int max_attempts = n1; // Maximum number of attempts allowed
// Reset the count for all elements to 0
for (Element& elem : rndnum) {
elem.count = 0; // Assuming count is a member variable of Element
}
for (int i = 0; i < n; ++i) {
// Using Mersenne Twister PRNG
std::uniform_int_distribution dist(0, n1 - 1);
int x = dist(gen);
rndnum[x].count++;
if (i > 0) {
// Check Element's Attributes (5) & Max Attempt to n1
if ((rndnum[x].value == rndnum[j + 1].value || haveSameAttributes(rndnum[x], rndnum[j + 1])) && attempts < max_attempts) {
int z = dist(gen);
++attempts;
// Compare Selection Count of Elements
if (rndnum[x].count > rndnum[i - 1].count && attempts < max_attempts) {
// Using Mersenne Twister PRNG
x = dist(gen);
++attempts;
rndnum[x].count++;
}
if ((x + z) >= j) {
x = (x + z) - j;
} else {
x = x + z;
}
}
}
std::swap(rndnum[x], rndnum[j]);
--n1;
--j;
}
}
// For counting the repetitions throughout the shuffled list
int countConsecutiveRepetitions(const std::vector& rndnum) {
int consecutiveRepetitions = 0;
for (size_t i = 1; i < rndnum.size(); ++i) {
if (haveSameAttributes(rndnum[i], rndnum[i - 1])) {
++consecutiveRepetitions;
}
}
return consecutiveRepetitions;
}
std::vector countConsecutiveRepetitionsPerAttribute(const std::vector& rndnum) {
int consecutiveRepetitions1 = 0;
int consecutiveRepetitions2 = 0;
int consecutiveRepetitions3 = 0;
int consecutiveRepetitions4 = 0;
int consecutiveRepetitions5 = 0;
for (size_t i = 1; i < rndnum.size(); ++i) {
if (rndnum[i].attribute1 == rndnum[i - 1].attribute1) {
++consecutiveRepetitions1;
}
if (rndnum[i].attribute2 == rndnum[i - 1].attribute2) {
++consecutiveRepetitions2;
}
if (rndnum[i].attribute3 == rndnum[i - 1].attribute3) {
++consecutiveRepetitions3;
}
if (rndnum[i].attribute4 == rndnum[i - 1].attribute4) {
++consecutiveRepetitions4;
}
if (rndnum[i].attribute5 == rndnum[i - 1].attribute5) {
++consecutiveRepetitions5;
}
}
int totalConsecutiveAttributes = consecutiveRepetitions1 + consecutiveRepetitions2 + consecutiveRepetitions3 + consecutiveRepetitions4 + consecutiveRepetitions5;
return {consecutiveRepetitions1, consecutiveRepetitions2, consecutiveRepetitions3, consecutiveRepetitions4, consecutiveRepetitions5, totalConsecutiveAttributes};
}
int main() {
const int desiredSize = 50;
const int numIterations = 1000;
std::vector baseList = {
{1, "Dragon", "Large", "Long", "Shoes", "Green"},
{2, "Dragon", "Small", "Average", "Boots", "Green"},
{3, "Dragon", "Small", "Short", "Slippers", "Red"},
{4, "Dragon", "Medium", "Average", "Shoes", "White"},
{5, "Dragon", "Large", "Long", "Boots", "Red"},
};
int totalConsecutiveRepetitions = 0;
std::vector totalConsecutiveRepetitionsPerAttribute(5, 0);
auto seed = static_cast(std::chrono::high_resolution_clock::now().time_since_epoch().count());
std::mt19937 gen(seed);
for (int iteration = 0; iteration < numIterations; ++iteration) {
std::vector rndnum;
// Repeat the base list to achieve the desired size
while (rndnum.size() < desiredSize) {
rndnum.insert(rndnum.end(), baseList.begin(), baseList.end());
}
// Trim the excess elements
rndnum.resize(desiredSize);
enhancedShuffle(rndnum, gen);
// Display each element with its attributes (Optional)
// for (const auto& element : rndnum) {
// std::cout
Подробнее здесь: [url]https://stackoverflow.com/questions/78190703/mersenne-vs-rand-why-do-i-get-more-total-consecutive-repetitions-when-using-mer[/url]