Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations cowski on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

copying random files in sub-folders

Status
Not open for further replies.

bigmike500

Mechanical
Oct 12, 2004
6
hi there
i am trying to write a program in c++ that will allow me when it runs to select a number of mp3 files at random from a folder and its sub-folders then copy them to my mp3 player. the problem that i am having at the moment is doing the random picking of files, does anyone know how and help me in how i can achieve this?
Thanks
mike
 
Replies continue below

Recommended for you

Hmm, doesn't look like this C++ category on Eng-Tips gets used all that much. I just joined and expected to see a bit more activity... :^)

I think what would be fine for your needs would be to use the standard C library function rand(). It returns a "random" integer; I use quotes around random because they are not truly random numbers. However, it should provide adequate behavior for your application, which sounds like you'd like a "random" selection of songs.

Some folks will also tell you that rand() has crummy statistical properties. That was true a decade or two ago for some C libraries, but most modern compilers have better libraries these days. If you're worried about this, there are literally hundred of random number generators available on the web. Knuth's "Art of Computer Programming" is a good starting area. Numerical Recipes in C has a number of generators in it. But for what you're doing, it's likely the built-in rand() function will do everything you need.

The usual technique is to divide rand() by double(RAND_MAX) and use the resulting number (which is >= 0 and < 1) to pick an integer from 0 to n-1, where n is the number of objects to choose from.

Finally, you probably want to select a sample from the set without replacement (i.e., hypergeometric sampling). See Knuth for ways of doing this or let me know and I'll post a routine here.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor