I'm trying to pick a random file from a folder tree, starting from a fixed path and "searching" recursively across all subfolders (or the choosen folder itself).
My idea is: make the list of files, calculate the number of files, choose a random number in this range and than pick the file at that index.
Here's my code:
// create list of all files
std::vector<std::string> paths;
for (const auto &entry : std::filesystem::recursive_directory_iterator(mPathDirectory)) {
if (!std::filesystem::is_directory(entry)) {
paths.push_back(entry.path().string());
}
}
// pick random file
size_t numberOfFiles = paths.size();
int indexRandomFile = (int)round(rescale(random::uniform(), 0.0, 1.0, 0, numberOfFiles - 1));
return paths[indexRandomFile];
Also with O3, its pretty slow, considering I've a huge list of files and I'm inside an "audio" application (which should be faster).
Do you have any smarter ideas? Somethings like O(1)? :P
Aucun commentaire:
Enregistrer un commentaire