Jump to content
  • Advertisement
Sign in to follow this  
_sim

Selecting a random file given a path.

This topic is 4871 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Anyone have any idea how to select a random file given a path to a folder that contains a set of files. I want to be able to load a random map for my game. The files are named 1.map - 10.map. I could just do a srand(time) and then rand % 11 and concat .map to that number. But I was hoping and wondering if there was a proper way to do it if the files were all not named in this manner. Also, anyone know if tracking the amount of milliseconds passed (using SDL_GetTicks()) there is a nice function out there that would return a formatted string in the format hh:mm:ss? I could write one, but why bother re-inventing the wheel? I know there's a time class, but it seems to work on that whole time elapsed since 1970 thing and return full strings including day, year, etc. Thanks! sim

Share this post


Link to post
Share on other sites
Advertisement
Regarding the first question, you can get _all_ the file names in the directory (FindFirstFile/FindNextFile or similar functions) that conform to your desired extension (.map in this case) into a std::vector<std::string>, and use rand() to get an array index.

there isn't really much of reinvention for the second question. if you have the amount of milliseconds passed, just convert it to seconds, minutes, and hours using a bit of math. 1000ms per second, 60s per minute, 60m per hour.
you can then use printf or similar formatter to write it in the form of
("%2d:%02d:%02d", hours, mins, sec)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!