fix rng bug

This commit is contained in:
2026-05-03 10:04:34 +02:00
parent 47ef3851ac
commit d449d09786
+18 -4
View File
@@ -196,9 +196,22 @@ void Shred::performExploration(Drive *drive)
size_t savedChunkSize = currentChunkSize; size_t savedChunkSize = currentChunkSize;
// Generate random chunk size between MIN and MAX (aligned to STEP boundaries) // Generate random chunk size between MIN and MAX (aligned to STEP boundaries)
size_t numSteps = (CHUNK_SIZE_MAX - CHUNK_SIZE_MIN) / CHUNK_SIZE_STEP_UP; // Calculate in MB to avoid overflow
size_t randomStep = rand() % (numSteps + 1); size_t minMB = CHUNK_SIZE_MIN / (1024 * 1024);
explorationChunkSize = CHUNK_SIZE_MIN + (randomStep * CHUNK_SIZE_STEP_UP); size_t maxMB = CHUNK_SIZE_MAX / (1024 * 1024);
size_t stepMB = CHUNK_SIZE_STEP_UP / (1024 * 1024);
// Number of possible steps: (max - min) / step
size_t numSteps = (maxMB - minMB) / stepMB;
// Generate random step: 0 to numSteps (inclusive)
// Using proper modulo to ensure range [0, numSteps]
int randVal = rand();
size_t randomStep = static_cast<size_t>(randVal) % (numSteps + 1);
// Calculate exploration chunk size in MB, then convert to bytes
size_t explorationMB = minMB + (randomStep * stepMB);
explorationChunkSize = explorationMB * 1024 * 1024;
// Clamp to valid range (safety check) // Clamp to valid range (safety check)
if (explorationChunkSize < CHUNK_SIZE_MIN) if (explorationChunkSize < CHUNK_SIZE_MIN)
@@ -213,7 +226,8 @@ void Shred::performExploration(Drive *drive)
// Enhanced logging with debug info // Enhanced logging with debug info
Logger::logThis()->info("EXPLORATION MODE: Testing " + Logger::logThis()->info("EXPLORATION MODE: Testing " +
to_string(explorationChunkSize / (1024 * 1024)) + " MB chunks " + to_string(explorationChunkSize / (1024 * 1024)) + " MB chunks " +
"(randomStep=" + to_string(randomStep) + "/" + to_string(numSteps) + ", " + "(randomStep=" + to_string(randomStep) + "/" + to_string(numSteps) +
", rand=" + to_string(randVal) + ", " +
"was " + to_string(savedChunkSize / (1024 * 1024)) + " MB, " + "was " + to_string(savedChunkSize / (1024 * 1024)) + " MB, " +
"best: " + to_string(bestChunkSize / (1024 * 1024)) + " MB)" + "best: " + to_string(bestChunkSize / (1024 * 1024)) + " MB)" +
" - Drive: " + drive->getSerial()); " - Drive: " + drive->getSerial());