I am trying to create a SkipList, which utilizes random numbers to determine height. My implementation has a flaw, where the following often results in over 20 elements consistently failing to have a local_height above 1.
local_height = 1;
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::minstd_rand0 randGen(seed);
int check = randGen() % 100;
while(check < probOutOf100 && (local_height <= height || local_height <= 4)){
local_height++;
check = randGen() % 100;
}
It seems that I can fix this through a workaround, but I'm worried that this will make my implementation slower.
local_height = 0;
unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
std::minstd_rand0 randGen(seed);
int successfulProb = -1;
int randomBound = 500;
while((successfulProb > (randomBound / 2) || successfulProb == -1) && (local_height <= height || local_height <= 4)){
successfulProb = 0;
for(int i = 0; i < randomBound; ++i){
if(randGen() % 100 < probOutOf100) successfulProb++;
}
local_height++;
}
Any ideas on what could be causing this bug or how to fix it? I can provide more of my code if needed. It seems that if I check the random numbers, it works consistently. Perhaps this is due to optimization?
Aucun commentaire:
Enregistrer un commentaire