mardi 8 août 2017

Fastest way to get a random file from a directy in C / C++

I want to have a random file from a directory, what I am currently doing is: Iterating over the files, and stop at a random point. I wonder if there is an easier solution, that might be faster.

My current code snippet:

std::string s
if ((dir = opendir (path)) != NULL) {
 while ((ent = readdir (dir)) != NULL ) {
   s = ent->d_name;
   //break on random occassions
   if(rand() > 0.5)
    break;
 }
 closedir (dir);
}else{
 perror ("");
 return EXIT_FAILURE;
}




Aucun commentaire:

Enregistrer un commentaire