How to load random file from folder?
#4

An example using FileManager but it has to read once to retrieve how many files exist and a second to select the random one:
pawn Код:
new dir: dHandle, item[40], type, counter, random_file;
   
dHandle = dir_open("path_here");

while (dir_list(dHandle, item, type))
{
    if(type == FM_FILE) counter++;
}
       
dir_close(dHandle);

random_file = random(counter);
counter = 0;
dHandle = dir_open("path_here");

while (dir_list(dHandle, item, type))
{
    if(type == FM_FILE && counter++ == random_file) break;
}

dir_close(dHandle);
printf("random file: \"%s\"", item);
If you know the number of files will not exceed a certain amount, an array to store the file names and avoid the second loop is also a choice.
Reply


Messages In This Thread
How to load random file from folder? - by theralio - 07.12.2018, 20:34
Re: How to load random file from folder? - by Jefff - 07.12.2018, 20:41
Re: How to load random file from folder? - by theralio - 07.12.2018, 20:46
Re: How to load random file from folder? - by Calisthenics - 07.12.2018, 21:00
Re: How to load random file from folder? - by theralio - 07.12.2018, 21:42
Re: How to load random file from folder? - by Calisthenics - 08.12.2018, 12:12

Forum Jump:


Users browsing this thread: 1 Guest(s)