У меня есть этот алгоритм перетасовки, я пытаюсь заменить Rand на Mersenne в качестве улучшения, поскольку Mersenne более эффективен и производит больше случайности по сравнению с rand, согласно тому, что я искал. Однако, сравнивая его рядом, я запускал его по 5 раз каждый и суммировал повторения каждого 5 случаев, при использовании Мерсенна я получаю больше повторений, чем в Ранде. Почему это? Я новичок в программировании, буду благодарен за вашу помощь. Спасибо!
Это мой код для использования Mersenne
У меня есть этот алгоритм перетасовки, я пытаюсь заменить Rand на Mersenne в качестве улучшения, поскольку Mersenne более эффективен и производит больше случайности по сравнению с rand, согласно тому, что я искал. Однако, сравнивая его рядом, я запускал его по 5 раз каждый и суммировал повторения каждого 5 случаев, при использовании Мерсенна я получаю больше повторений, чем в Ранде. Почему это? Я новичок в программировании, буду благодарен за вашу помощь. Спасибо! Это мой код для использования Mersenne [code]#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; };
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}; }
// 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
Я новичок в Python и пробую Turtle с помощью руководств. Этот вопрос оказался многословным, моя вина.
Я разработал 2 игры, обрабатываемые 2 функциями (в отдельном модуле), которые вызываются из MAIN:
F1: играет в игру «Зарисовка эскиза», в которой...
Я пытаюсь использовать многопоточность в Python, чтобы просмотреть большой файл текста (символов) и подсчитать повторения одних и тех же символов. По сути, это воссоздание метода string.count(char), но вместо просмотра строки он будет проходить...
Я хочу перебирать список значений вперед и назад. Проблема в том, что когда я меняю направление, я получаю тот же элемент
List strings = Arrays.asList( 1 , 2 , 3 );
ListIterator listIterator = strings.listIterator();
String first =...